You will undoubtedly need to use variables in your PowerShell scripts from time to time fortunately PowerShell has you covered. There are many built-in variables you can use to provide such information as the home folder ($pshome) or the current working folder ($pwd). If you would like to see a list of the built in and user defined variables available to the current session type;
dir variable: | sort name
The dir alias is used to reference the get-childitem cmdlet, which takes variable as it’s argument. Get-childitem then pipes those values to the sort-object cmdlet which is aliased by sort, which sorts the output by the variable’s names. When you reference a variable in your code you usually will need to precede the variable name with $. There are only a few cases where you would not need to use $ in front of a variable name. If you just want the value of one built in variable you can enter just that variable’s name. for example;
this will display the path to the powershell home folder.
You can use built in variables with statements also.
dir $pshome -filter *.dll
as you can see this statement uses $pshome to retrieve a list of all the .dll files in the powershell home directory.
PowerShell has 2 types of built in variables, preference and automatic. Preference variables can have their values changed unlike automatic variables. You can get more information on both preference and automatic variables by reading their help files.
To change the value of a preference variable you use = which is the assignment operator.
$maximumerrorcount = 260
the new value is used until you reassign the value or end your session.
Windows environment variables are accessed by using the Env drive.
dir env: | sort name
Environment variable are prefixed with $env instead of just $ when you reference them in your PowerShell code.
When you type this command you see that it returns the directory where windows resides. To retrive a list of all the .dll files in that directory type;
dir $env:windir -filter *.dll
You can also change the values of environment variables.
type $env:homepath to see the current home directory path
$env:hompath = $env:homepath + “home”
now you can see that your path has changed to include home at the end of your path.
PowerShell does not require that you explicitly declare a variable before you use it.
$var1= “one two”
The previous command sets the string “one two” to the $var1 variable. When you set a variable this way you are actually calling the New-Variable cmdlet and providing it the argument “one two”.
New-Variable var1 “one two” does exactly the same thing, but this time is not preceded with a dollar sign. When a variable’s name is an argument to the New-Variable cmdlet or to the Set-Variable, Clear-Variable or Remove-variable cmdlets, you do not need to include the dollar sign.
new-variable var1 “one two” -option readonly
Here we have specified that ReadOnly is an argument to the -option parameter. If you specify more then one argument then you need to use commas to separate them. see the New-variable help file for more info. Most of the time the simplest way to change a variables value is to use =.
$var1 = “three”
you can also use;
Set-variable var1 “three”
Both of these statements will set the value assuming that the variable has not been defined as read-only. If it has been set to read-only then it will result in an error. You can get around this by using the -force parameter.
set-variable var1 “three” -force
the advantage to using the Set-variable cmdlet is the ability to additional parameters. Sometimes you need to clear a variable’s value. One way of doing so is by using the built in variable $null
$var1 = $null
You can also use the Clear-Variable cmdlet;
But of course if you have set the variable to read only you will need to force it.
Clear-variable var1 -force
Lastly if you need to delete a variable you can use the Remove-variable cmdlet
remove variable var1
or if read only
remove -variable var1 -force
Lets look at another example;
$var1 = “one two” sets the string “one two” to the $var1 variable.
$var1 retrieves the value of the variable $var1
$var1.gettype() verifies the object type which should be a string
we can also assign an integer to a variable
$var1 = 123 sets the value to an integer
$var1.gettype() verifies that the object type is integer
you can also write the prior two commands as;
$var1 = 123; $var1.gettype() you can write these commands this way because you can use semicolons to manually terminate a statement.
you can append a value to a string like so;
$var1= “one two three”; $var1
$var1 = $var1 + ” four”; $var1
PowerShell can use variable in strings, however the way in which the variables are treated depends on whether you use single or double quotes. If you use single quotes powershell outputs the variables name as entered. If you use double quotes then powershell outputs the variables value.
$svc = “eventlog” sets the variable $svc with the value “eventlog”
write-output “the service is $svc.” powershell sees the $ and retrieves the value of $svc which is “eventlog”
but if we use single quotes
write -output ‘the service is $svc.’ powershell sees the variable as a litteral value and does not return the stored value of the variable, but instead displays the name of the variable itself.
you can also include both the variable name and the value
write-output ` “the value of `$svc is $svc”