Using variables
In PowerShell, the data is stored, retrieved, and modified using the methods listed next. In this section, we are going to cover only variables:
- A variable is used to store bits of information
- Arrays are used to store information in an index
- A Hash table uses key-value pair to store data
A variable is virtual storage to store information that will be utilized later in a script or the information that is the output of running a script. Variables can contain different data types such as text strings, objects, and integers. There are few special variables that you can use, which are defined in PowerShell.
The examples of some of the special variables are as follows:
Special Variable |
Description |
---|---|
|
This is the same as the $PS item. It contains the current object in the pipeline object and can be used in commands that perform an action on all or selected objects in a pipeline. |
|
This contains the path of the user's' home directory, typically |
|
This is an array that contains the errors in the current execution. The first element in the array ($Error[0]) will contain the most recent error. |
|
This is an automatic variable that contains a NULL or empty value. |
If you are looking for a full list of automatic variables available for use in Windows PowerShell, type the following:
PS C:\> get-help about_automatic_variables
In PowerShell, all the variable names must start with the $
character. Data is assigned to a variable using the =
operator:
PS C:\> $Number = 25
There are the Set-Variable and Get-Variable cmdlets that can also be used to set and retrieve the values of the variable.
Here, the value 401B is passed to the variable ExamCode
:
PS C:\> Set-Variable -Name ExamCode -Value 401B PS C:\> Get-Variable ExamCode
A little bit about operators that are used for assignment and comparison of values assigned to variables.
Assignment operators
The following operators are used to assign values to variables and carry out numeric operations before the assignment:
Operator |
Description |
---|---|
|
A specific value is assigned to a variable using the equal to |
|
The variable value is increased by a specific number |
|
The variable value is decreased by the number specified |
|
This operator is used to multiply the value stored in a variable by a specified number |
|
This divides the value by a specific number |
|
In this case, the value stored in the variable is divided by the specified number and the modulus (remainder) is stored in the variable |
|
This increases the value of variables by 1 |
|
This decreases the value of variables by 1 |
The example assigns the value of to the variable called $Book
:
$Bookk = "Microsoft Exchange PowerShell Essentials"
In the previous example, the $Book
variable is created as we have assigned the value to the variable using the assignment operator. If you look at the following example, the first statement creates a $x variable and assigns a value of 12. The second statement has changed its value to 34:
$x = 12 $ = 34
While scripting, there is a need to combine operations such as addition, subtraction, and assignment to variables. For example, the following statements will produce the exact same output:
$i += 3 $i = ($i +3)
Similarly, if you want to multiply first and then assign, use any of the following statements:
$n *= 4 $n = ($n * 4)
If you want to use a specific data type, you have to override how PowerShell stores values in variables. This can be achieved by strongly typed variables or casting them. For example, the first one is a variable that will only store integers and the second one will store strings:
[int]$Number = 12 [string]$Book = "Microsoft Exchange PowerShell Essentials"
Now, let's take a look at an example of what happens if we do not specify the correct datatype to a variable. Here, we have stored the value of 56 in the $x
variable as a string. The next statement adds the value of 8 to the variable. The output will be displayed as a string and numeric operation will be performed. As a best practice, you should always cast your variables with the correct data type:
[string]$s = 56 $s += 8 $s 568
You will not be able to recast a variable without changing its value. In our previous example, we have casted $Book
as a string value and stored the value of Microsoft Exchange PowerShell Essentials in it. If you try to use the following statement, you will get an error message, which essentially means that you have to change the value before you cast the variable to a different data type:
[int]$Book
Cannot convert value string
to type System.Int32
. Error—"Input string was not in a correct format."
At line:1 char:1 + [int]$b + ~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : InvalidCastFromStringToInteger
Now, let's use the Windows PowerShell help system to learn more about other assignment operators:
PS C:\> get-help about_assignment_operators
Comparison operators
These operators are used to compare values and match specific patterns. Here is the list of comparison operators available, and these operators are case insensitive. If you want to make these operators case sensitive or insensitive, precede c
or i
in front of the following operator:
eq
-ne
-gt
-ge
-lt
-le
-Like
-NotLike
-Match
-NotMatch
-Contains
-NotContains
-In
-NotIn
-Replace
Take a look at the following examples:
-eq
—Equal to:
PS C:\> "apple" -eq "apple" True PS C:\> "apple" -eq "orange" False
-ne
—Not Equal to:
PS C:\> "apple" -ne "apple" False PS C:\> "apple" -ne "orange" True
-gt
—Greater than:
PS C:\> 5 -gt 8 False PS C:\> 23,34,56 -gt 50 56
Like
—Match using the wildcard character (*).
Take a look at the following examples:
PS C:\> "Windows PowerShell" -like "*shell" True PS C:\> "Windows PowerShell", "Server" -like "*shell" Windows PowerShell
Review the help for other comparison operators:
PS C:\> get-help about_comparison_operators