top of page
Writer's pictureThe Tech Platform

What is a PowerShell variable

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:

$vrb = 201 

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


Example 2:

$mySubject = "PowerShell" 

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:


Example:



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.


Example:



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.




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.


Example:





Type of a variable

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




Variable Scope

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

$[<scope-modifier>:]<name> = <value> 

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:

$Global: variable = <value> 
  • 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:

$variable = <value> 
  • 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:

$script: variable = <value> 


Types of Variables

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

  1. User-Created Variables.

  2. Automatic Variables.

  3. Preference Variables.

1. 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.


2. 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.


3. 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.



Source: Javapoint


The Tech platform

0 comments

Comments


bottom of page