Javatpoint Logo
Javatpoint Logo

PowerShell Variables

Variables are the fundamental part of the Windows PowerShell. We can store all the types of values in the PowerShell variables. For example, we can store the result of commands, and the elements which are used in expressions and commands, such as paths, names, settings, and values. In fact, they store the objects specifically, Microsoft .NET Framework objects.

A variable is a unit of memory in which the data is stored. In Windows PowerShell, the name of a variable starts with the dollar ($) sign, such as $process, $a. The name of the variables are not case-sensitive, and they include spaces and special characters. By default, the value of all the variables in a PowerShell is $null.

Note: In Windows PowerShell, special characters have special meaning. If we use the special characters in the variable names, we will need to enclose them in the braces {}.

Valid variable names: Invalid variable names:
$myVariable, myVariable,
$MyVariable_1, $my-variable,
{my-variable} $my variable

Create a variable

We use an assignment operator (=) to assign a specified value to the variable. We can create a variable by assigning it a value.

Following examples are used to create a variable:

Example 1:

The command in this example assigns the integer value 201 to the variable called $vrb.

Example 2:

The command in this example creates a variable named $mySubject and assign it a string value. In this example, $mySubject is a string object.

Print the value of a variable

To display the value of a variable, type the name of a variable, followed by a dollar sign '$'.

Following example is used to print the value of a variable:


PowerShell Variables

The second command $a in this example displays the value of variable as "javatpoint".

Change the value of a variable

If you want to change the value of a variable, assign a new value to that variable.


PowerShell Variables

The $PowerShell command in the above screen displays the value of a $PowerShell variable.

The commands in the following screen change the value of a $PowerShell variable and display the new value of a $PowerShell variable.

PowerShell Variables

Delete a variable

If you want to delete the value of the variable, use the clear-variable cmdlet, or change the value of it to $null.


PowerShell Variables
PowerShell Variables

Type of a variable

If you want to find the type of a variable, you can use the GetType() method.

PowerShell Variables

Variable Scope

PowerShell variables can have a "scope" which determines where the variable is available. To denote a variable, use the following syntax:

Windows PowerShell supports the following scope modifiers of a variable:

  • Global: Global variables are those variables that are valid everywhere, even outside the scripts and functions. To denote global variables, use the following format:
  • Local: Those variables which can be created in a local scope. By default, the variable has a local scope. To denote a local variable, use the following format:
  • Script: Those variables which are created during the script. These variables are only available to that script in which they are created. To denote a script variable, use the following format:

Types of Variables

Following are the different types of variables in the Windows PowerShell:

  1. User-Created Variables.
  2. Automatic Variables.
  3. Preference Variables.

User-created Variables

Those variables which are created and maintained by the user are called user-created variables. The variables that we create at the PowerShell command line exist only while the Window of PowerShell is open. When the Window of PowerShell is closed, the variables are also deleted. We can create the variables in the scripts with the local, global, or script scope.

Automatic Variables

Those variables which store the state of PowerShell are called automatic variables. The PowerShell creates this type of variable, and their values are changed by the PowerShell to maintain their accuracy. The user cannot change the values of these variables.

Preference Variables

Preference variables are those variables that store the user preferences for the Windows PowerShell. The Windows PowerShell creates this type of variable, and they are populated with the default values. Any user can change the value of preference variables.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA