PowerShell Variables
A variable is a name given to a particular area in memory, that stores a value, which can later be reused. Variable names in PowerShell can contain letters, numbers and underscores, but always start with a ‘$’ sign. It is possible to declare a variable with or without specifying the type of data that it can store, such as string, integer or floating-point number. Where no data type is specified, PowerShell allocates a type based on the data that is assigned to it. In this situation, the data type of a variable can change if a value that is of a different type is assigned. In order to fix the data type of a variable, it must be explicitly stated when it is declared.
When creating a variable, it takes the following format. The equals sign between the variable name and value is known as an assignment operator and will be discussed further in the next section on operators.
# Variable without a data type specified. $variable_name = value # Variable with a data type specified. [variable_type] $variable_name = value
Below are some example variables, the last of which is a boolean variable, that can either be ‘true’ or ‘false’. Note that, when assigning ‘true’ or ‘false’ to this type of variable, it must be preceded by a ‘$’ sign. It isn’t necessary for the words ‘true’ and ‘false’ to be in capitals, but it is good practice to do so, in order to distinguish them from variables. It is also good practice to use camel case when naming variables, for example ‘$camelCase’, where the first letter of the first word is in lower case, then any words in the name thereafter start with an uppercase letter.
# Integer variables. $exampleInteger1 = 5 [int] $exampleInteger2 = 10 # Floating point or decimal number variables. $exampleFloat1 = 5.0 [double] $exampleFloat2 = 10.0 # String variables. $exampleString1 = "This is a string" [string] $exampleString2 = "This is another string" # Boolean variables. $exampleBoolean1 = $TRUE [boolean] $exampleBoolean2 = $FALSE
As well as being able to assign user defined values to a variable, it is also possible to assign system values. This can be demonstrated with a variable of type ‘datetime’, where the ‘Get-Date’ cmdlet can be used to assign the current date and time.
# DateTime variables. $exampleDateTime1 = Get-Date [datetime] $exampleDateTime2 = Get-Date
The examples shown here are just a small number of the types of variables that are available.
As well as variables being declared and initialised inside a script, parameter values can also be passed in and used the same as a variable declared within.
param ( [string] $pName ) Write-Host "Hello $pName"
The 'param' section is used to specify parameters that are expected to be passed in, in this case 'pName'. The script can be called with the parameter 'pName' specified as follows.
PowerShell.exe -ExecutionPolicy Bypass -File .\demo.ps1 -pName "Fred"
This will produce the following output.
Hello Fred
If the expected parameter was not passed in, a default value can be specified in the script that can be used instead.
param ( [string] $pName = "World" ) Write-Host "Hello $pName"
If no parameter is passed, it will produce the following output.
Hello World
It should be noted that in the 'param' section, the data type doesn't need to be specified, however, if specified, this ensures that the value passed in is of the correct type, otherwise an error is produced.