Finding Creating Using Variables

Variables as you now, are used to store some value for use throughout the operation/action we are performing. In powershell you can use a variable to save an entire command and use it repeatedly whenever needed.

Get-Variable cmdlet is used to list down all built in powershell variables.

(Full list is not shown in screenshot)

We need to use ‘$’ as prefix for using variables. By using $, we are telling powershell that we are using a variable. For example:

There is another section of variables in powershell called Environment Variables. For viewing the list of environment variable, first you need to set powershell working location to Environment PSDrive and then get the list of child items.

(Screenshot is not having the full list)

You need to use ‘$env:’ as prefix while using environment variables in powershell so as to tell that the variable we are trying to use is an environment variable.

(You can use tab complete to find through different environment variables)

You can also create your own variables in powershell. You can do so by using New-Variable cmdlet.

While using this cmdlet, you do not need to have $ prefix with variable name and value is specified using â€"Value parameter. Below is the list of all parameters of New-Variable cmdlet:

-Name : Used to specify name of the variable.

-Value : Used to specify value at the time of creation.

-Description : Description for the variable being created.

-Option : Used to set different options such ReadOnly, Private etc for variable.

-Visibility : Used to specify whether the variable is visible outside the session in which it was created.

-Force : Used to overwrite an existing variable.

-PassThru : Returns an object which currently is in use

-Scope : Specifies the scope of the variable.

-WhatIf : Tells what would happen when the cmdlet is executed.

-Confirm : Prompts a confirmation message before executing cmdlet.

Alternatively, you can create a variable by using $ prefix and assigning some value as shown below:

As stated before, you can store an entire command(s) in a variable. See screenshot below: