Windows powershell scripting

· Programming language · Computer programming · Microsoft Windows · Powershell sink ·


Two flavors of PowerShell

Getting help on everything

Get-Help <term>

get-help <cmdlet-name> -online # get help from the online MS library

Get-Command -Noun Variable # To get help for a specific cmdlet

Also Powershell sink



Alphanumeric variable names can contain these characters:

Special character variable names can contain these characters:

PowerShell has reserved variables such as $$$?$^, and $_ that contain alphanumeric and special characters. For more information, see about_Automatic_Variables.

${save-items} = "a", "b", "c"

# To get the child items in the directory that is represented by the `ProgramFiles(x86)` environment variable
Get-ChildItem ${env:ProgramFiles(x86)} 

# To reference a variable name that includes braces, enclose the variable name in braces, and use the backtick character to escape the braces. For example, to create a variable named `this{value}is` type:
${this`{value`}is} = "This variable name uses braces and backticks."

Declaration and types

No declaration required.

PowerShell variables are loosely typed, which means that they aren't limited to a particular type of object. A single variable can even contain a collection, or array, of different types of objects at the same time.

The data type of a variable is determined by the .NET types of the values of the variable. To view a variable's object type, use Get-Member.

$a = 12 # System.Int32
$a = "Word" # System.String
$a = 12, "Word" # array of System.Int32, System.String
$a = Get-ChildItem C:\Windows # FileInfo and DirectoryInfo types

# Type casting
[int]$number = 8
$number = "12345" # The string is converted to an integer
$number = "Hello" # THe result: Cannot convert value "Hello" to type "System.Int32". Error: "Input string was not in a correct format."

[datetime] $dates = "09/12/91" # The string is converted to a DateTime object
$dates = 10 # The integer is converted to a DateTime object

To get a list of all the variables in your PowerShell session, type Get-Variable

$ sign is used to reference variables

# Assigning variables
$MyVariable = 1, 2, 3
$Path = "C:\Windows\System32"

# In one statement
$a = $b = $c = 0

# To clear the value of a variable
Clear-Variable -Name MyVariable
$MyVariable = $null

# To delete a variable
Remove-Variable -Name MyVariable
Remove-Item -Path Variable:\MyVariable

The scope

A variable that you create in a function is available only within the function. A variable that you create in a script is available only within the script. If you dot-source the script, the variable is added to the current scope. For more information, see about_Scopes.

To modify the scope

$Global:Computers = "Server01" # The variable has a global scope, even when it's created in a script or function.

The Variable: drive

The PowerShell Variable provider creates a Variable: drive that looks and acts like a file system drive, but it contains the variables in your session and their values.

Set-Location Variable: # To change to the `Variable:` drive

Get-ChildItem Variable: # To list the items and variables in the `Variable:` drive

Get-Item Variable:\PSCulture #To get the value of a particular variable

Environment variables

Environment variables are always strings
An environment variable can't be an empty string
Syntax $Env:<variable-name>, to create/update $Env:<variable-name> = "<new-value>"

$Env:windir # get

$Env:Foo = 'An example' # create/update

# Also possible to operate on envs using Item
Copy-Item -Path Env:\Foo -Destination Env:\Foo2 -PassThru
Set-Item -Path Env:\Foo2 -Value 'BAR'
Get-Item -Path Env:\Foo*
Remove-Item -Path Env:\Foo* -Verbose

# Also using the System.Environment methods
[Environment]::SetEnvironmentVariable('Foo','') # To remove a variable

# The scope of an env var
[Environment]::SetEnvironmentVariable('Foo', 'Bar', 'Machine') # Or 'User'

PowerShell environment variables

Some of them