Many Windows Administrators use PowerShell for its shell purposes but anyone with a scripting or programming background will want to use it for its scripting capabilities. In this post I’ll cover the basic usage of variables in PowerShell.
What are Variables?
Variables are nothing more than placeholders for information or for a value. Each variable is defined with a name and the name is used as a reference to whatever is stored in the variable. Variables are useful to store information you want to use later, or to cache the information so you don’t have to keep querying the same information over and over again. Any type of data may be stored in a variable.
Usage in PowerShell
PowerShell variables always start with a dollar sign ($) and are followed by the variable name like this: $variable. Values are assigned using the equals operator (=). Note that variable names in PowerShell are not case-sensitive. $hello and $Hello are the same thing. People with experience from strongly typed languages may notice there is no need to explicitly define the datatype. although you could type [int]$number = 1, there is no need as PowerShell will figure it out. It is useful in certain circumstances though.
Let’s try it:
In the example above, we created a variable called $hello and assigned the value “Testing variables!”. We then created another variable called $number and assigned the value 123. Notice the arithmetic operation at the end where we used our variable $number and added 1.