Variables are units in memory that hold different values. Both built-in and user-defined variables are available in PowerShell.
Note
All PowerShell variables start with the dollar sign ($).
The following table shows some of the commonly used variables built-in to Windows PowerShell.
Built-in Variables | Comments |
---|
$pshome PS C:\> cd $pshome PS C:\Windows\System32\ WindowsPowerShell\v1.0>
| Location of PowerShell command and associated configuration files. A profile can be placed here.
The example changes the directory to the location of $pshome. |
$profile PS C:\> $profile C:\Users\Darril\Documents\ WindowsPowerShell\Microsoft. PowerShell_profile.ps1 PS C:\> test-path $profile
| Shows the perceived location of the profile.ps1 file. This path does not exist by default. The test-path $profile command returns false if it does not exist and true if the file does exist. |
$error | An array of all the errors from the current session. The most recent error can be retrieved with $error[0] (the first item in the array). |
$_.
PS C:\> get-service | where {$_.status -eq "stopped" }
| The $_. string is a special variable that indicates the current cmdlet being piped.
In the example, the get-service command retrieves all services and is then piped to the where clause. The $_. variable is used with dot notation to identify the status of each service and see if they are stopped.
Note
This effectively works like a foreach clause. It lists the information for each service that matches the status of stopped.
|
$pwd | Current working directory. |
Note
You can retrieve a full list of variables with the get-variable cmdlet or the variable alias. The output lists the variables and their current values.
You can also create your own variables. Variables
are assigned with the equal sign (=) and prefixed with the dollar sign
($). You can then use the variable in the current session or within a
script.
The following table shows the methods used to assign and manipulate variables.
Assigning Variables | Comments |
---|
PS C:\> $d = get-date PS C:\> $d
| Assigns the current date and time value to $d.
You can view the value of the variable by entering $d by itself. |
| Assigns the value 0 to a variable. |
PS C:\> $counter = $counter + 1 PS C:\> $counter++ PS C:\> $counter
| Increments a variable by 1.
If the $counter
started as 0, the first line would add 1 to it, and the second line
would add 1 again. The third line would show the value is 2. |
PS C:\> $counter = $counter - 1 PS C:\> $counter-- PS C:\> $counter
| Decrements a variable by 1.
If the $counter
started as 0, the first line would subtract 1 from it and the second
line would subtract 1 again. The third line would show the value is -2. |
PS C:\> $msg = "Success!" PS C:\> $msg
| Assigns a string of characters to a variable.
You can view the value of the variable by entering $msb by itself. |
When working with numeric variables, you can use different mathematical assignment values, as shown in the following table.
Working with Numeric Variables | Comments (Including the Value $x if it Starts with 10) |
---|
| Assignment. The value of $x is 10 after this command. |
| Addition. If $x started at 10, the value of $x would be 15 after this command. |
| Subtraction. If $x started at 10, the value of $x would be 5 after this command. |
| Multiplication. If $x started at 10, the value of $x is 50 after this command. |
| Division. If $x started at 10, the value of $x is 2 after this command. |
| Module (remainder). If $x started at 10, the value of $x is 1 after this command. |
| Additive assignment. If $x started at 10, the value of $x is 20. |
| Subtractive assignment. If $x started at 10, the value of $x is 0 after this command. |
| Multiplicative assignment. If $x started at 10, the value of $x is 100 after this command. |
| Quotient assignment. If $x started at 10, the value of $x is 1 after this command. |
| Remainder assignment. If $x started at 10, the value of $x is 1 after this command. |
| Increment. If $x started at 10, the value of $x is 11 after this command. |
| Decrement. If $x started at 10, the value of $x is 9 after this comm |