Lessons in PowerShell - Variables and Declarations
The following useful information is primarily taken from the Windows PowerShell Language Quick Reference (QuadFold.rtf) documentation.
Automatic Variables
PowerShell includes the following in-built variables:
Variable | Description |
---|---|
$$ | Last token of the previous command line |
$? | Boolean status of last command |
$^ | First token of the previous command line |
$_ | Current pipeline object |
$Args | Arguments to a script or function |
$Error | Array of errors from previous commands |
$Foreach | Reference to the enumerator in a foreach loop |
$Home | The user’s home directory; usually set to %HOMEDRIVE%%HOMEPATH% |
$Host | Reference to the application hosting the POWERSHELL language |
$Input | Enumerator of objects piped to a script |
$LastExitCode | Exit code of last program or script |
$Matches | Hash table of matches found with the -match operator |
$PSHome | The installation location of Windows PowerShell |
$profile | The standard profile (may not be present) |
$StackTrace | Last exception caught by Windows PowerShell |
$Switch | Enumerator in a switch statement |
$True | Boolean True |
$False | Boolean False |
$Null | Null |
Variable Declaration
Variables and other data elements may be instantiated in different scopes:
- Variables in the global scope are visible in all scopes.
- Variables in the script scope are visible to all scopes within that script file.
- Variables in the local scope are visible only in the current scope and its children.
- Private scope variables are visible only to that current scope.
A scope is created in the body of a shell function.
Format:
1$[scope:]name or ${anyname} or ${any path}
Examples:
1$a = 1
2$global:a = 1 # Visible everywhere
3$local:a = 1 # defined in this scope and visible to children
4$private:a=1 # same as local but invisible to child scopes
5$script:a=1 # visible to everything in this script
6$env:path = "d:\windows"
7${C:\TEMP\testfile.txt}="This writes to a file"
8Get-Variable -scope 1 a #Gets value from the parent scope
9Get-Variable -scope 2 a # grandparent
Type Declaration
Variables also can be declared as specific data type by prefixing the variable declaration with the data type.
Type | Description |
---|---|
[bool] or [boolean] | A boolean (True or False) value |
[byte] | An 8-bit unsigned character |
[char] | A Unicode 16-bit character |
[string] | String of Unicode characters |
[datetime] | A System.DateTime object |
[int] | A 32-bit signed integer |
[long] | A 64-bit signed integer |
[single] | A Single-precision 32-bit floating point number |
[double] | Double-precision floating number |
[decimal] | A 128-bit decimal value |
[xml] | A xml object |
[array] | An array of values |
[hashtable] | A System.Collections.Hashtable object |
[wmi] | Windows Management Instrumentation (WMI) instance or collection |
[wmiclass] | WMI class |
[adsi] | Active Directory Services object |
A variable’s type also can be declared as a .NET Framework class by using the full class name. For example:
1[System.Int32] $amount = 1234