C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
PowerShell VariablesVariables 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 {}.
Create a variableWe 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 variableTo 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 "TheDeveloperBlog". Change the value of a variableIf 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 variableIf 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 variableIf you want to find the type of a variable, you can use the GetType() method. Variable ScopePowerShell 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: variable = <value>
$variable = <value>
$script: variable = <value> Types of VariablesFollowing are the different types of variables in the Windows PowerShell:
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.
Next TopicAutomatic Variables
|