TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PowerShell Variables

PowerShell Variables with What is PowerShell, History of PowerShell, Features of PowerShell, PowerShell vs CMD, PowerShell Scripting, PowerShell Versions, PowerShell Commands, PowerShell Looping, PowerShell Conditions, PowerShell Array, PowerShell Brackets etc.

<< Back to POWERSHELL

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:

$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:

PowerShell Variables

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

Change the value of a variable

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

Example:

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.

Example:

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:

$[<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.

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.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf