Last Updated on February 8, 2024 by Satyendra
In any programming language, a variable represents a storage location for a data value that you can reuse later in your code. Using variables makes it easy to change values in multiple places as you can change the variable’s definition and this new definition will be used throughout your code. Information such as names, paths, and the results of commands can be stored in a variable and the variable type (for example, string, integer and so on) will depend on what kind of data this is.
In this article, we will look at how to name a variable, assign values to a variable and the different data types of a variable.
Naming a PowerShell Variable
A PowerShell variable name is always preceded by the dollar sign “$” and can only contain letters, numbers, and the underscore character. If you need to use other characters, they need to be enclosed in curly brackets “{}”. You should not use variable names that are pre-defined.
The following are examples of valid names:
- $myVariable
- $MyVariable_1
- ${my-variable}
and these are invalid names:
- myVariable
- $my-variable
- $my variable
A key point to be aware of is that the variable name starts after the dollar sign. This is essential to take into account because when you specify the variable name as a parameter in a cmdlet, you need to enter it without the dollar sign. The dollar sign tells the shell to read the variable’s value.
Together with most keywords in PowerShell, variables are not case sensitive. So, PowerShell does not distinguish between $myVariable and $Myvariable.
Assign Values to A PowerShell Variable
You can assign values to a PowerShell variable by combining a variable name, an assignment operator, and an expression. Here is an example:
$a = 1 + 1
The “=” sign is one of eight assignment operators. An expression is everything for which PowerShell can determine a value.
Other examples of assigning a value to a PowerShell variable are:
$a = “Hello World”
$b = 1
If you enter an expression at a PowerShell prompt, PowerShell returns its value.
To display the value of a variable, you don’t need a special command as entering the variable name is enough. This works in a script and on a command prompt.
Thanks to the variable interpolation, you can also expand a variable in a string if the string is enclosed in double quotation marks.
“These are the values of the variables: $a, $b.”
If you want to display the variable names as text instead of displaying their values, you have to enclose the string in single quotation marks.
‘These are the names of our variables $a, $b.’
There is no interpolation with single quotation marks:
In this case, PowerShell doesn’t recognize $a and $b as variables—just as ordinary strings.
PowerShell Data Types
In the examples above, we have used only two data types: strings and integers (32-bit integers). PowerShell supports many other data types, such as floating point numbers, strings, and Boolean values. You don’t have to explicitly declare the data type of a variable as variable types in PowerShell are determined automatically when they are assigned a value.
This means that a data type is selected based on your assigned data. For example, for a variable $age=20 then the int32 data type will be assigned and for a variable $student =” Michael” then a string data type will be assigned.
At different times, a variable may be associated with values of different types either through assignment or by the use of the ++ and ‑‑ operators. When the value associated with a variable is changed, that value’s type may change.
For example:
$i = “xyz” | $i holds a value of type string |
$i = 2147483647 | $i holds a value of type int |
++$i | $i now holds a value of the type double because 2147483648 is too big to fit into type int |
Any use of a variable that has not been created results in the value $null. To see if a variable has been defined, you can use the Test-Path cmdlet.