Powershell variables
Variables in PowerShell are like boxes where you can store information. You can put anything in these boxes, like numbers, text, or even the results of commands. You identify these boxes with a dollar sign ($) followed by a name. For example, $name or $age.
These boxes are flexible; you can store different types of things in them. They’re not case-sensitive, so $name and $Name are the same box. If you don’t put anything in a box, it’s empty, or in PowerShell terms, it’s $null.
Note: – in windows PowerShell, special characters name special meaning. If we use special characters
In the variable names, we will need to enclose them in braces {}.
Creating 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.
Example1
$vrb = 201 # this command is the example of assigning the integer value 201 to the variable called $vrb
Example2
$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 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 the example displays the variable as 200.
Delete a variable
If you want to delete the value of variable, use the clear- variable cmdlet, or change the value of it
To $null.
Example
Type of variable
If you want to find the type of variable, you con 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:
- User-Created Variables.
- Automatic Variables.
- Preference Variables.
User-Created Variables
Imagine you have a toolbox. You can create your own tools (variables) to use for specific tasks. When you close the toolbox, those tools disappear. Similarly, when you close the PowerShell window, the variables you created within it vanish.
Automatic Variables
PowerShell has its own built-in tools (automatic variables) that it uses to keep track of things. You can’t change these tools; they’re there to help PowerShell function correctly.
Preference Variables
These are like your personal settings in PowerShell. You can customise
them to your liking, such as how you want PowerShell to display information or how it should behave.
The Following table lists the preference variables and their default values:
PowerShell Array
An array in a PowerShell is a data structure that is used to store the collection of values in a single variable. The values in an array can be the same type or the different types. A value can be a string, an integer, a generic object, or another array.
Each element or a value in an array has an index. Indexes are the integers which represent the place of a value in an array. We can retrieve the elements of an array using the index. The index of an array always starts with 0, which indicates the first item of an array.
Creating and Initializing an array
The following statement is a syntax to declare the array variable:
$variable_name = value1, value2, value3, ….. ,ValueN
To create and initialize an array $k, which contains the five integer values: 10, 15, 20, 11, 5. Type the following command in the PowerShell:
$k = 10, 15, 20, 11, 5
Accessing Array Elements
- You can display all the values of an array on the PowerShell console by typing the name of an array after a Doller sign.Example – $k
- We can also access the elements from an array by using the index number. Enclose the index number in a square bracket.Example – $k[3] #It will display the data stored at index 3.
- We can access the part of an array by using the range operator for the index.
you want to access from the third element to the sixth element of an array, you would type the following command:Example – $k[2..5] - We can access the last part of the array by using the negative numbers, which are used to count the elements from the end of an array. The number “-1” denotes the last element of an array.Example – $k[-1] #to display last element of array.
Size of an array
An Array has a size according to the number of elements, you can get the size of an array by using #variable_name.Length
Example – $k.Length
Manipulation of an Array
- We can change the value of specific index value in an array by specifying the name of the array and the index number of value to change.
Example – $k[2]=20
- We can also add the value to the array by using the += operator.
Example – $k += 5
Initializing an empty array
We can initialize an empty array by using the following syntax:
$variable_name = @()
Example – $a = @()
Remove element from an Array
You can understand how to remove a single or more than one element from an array by using the following example:
$array = ($array[1] $array[2] $array[3])
PowerShell Hash table
In PowerShell, a Hashtable is a way to store information using pairs of keys and values, much like a dictionary or an associative array. Each Hashtable you create is actually a Hashtable object behind the scenes. You can use various properties and methods that come with this object to work with your data. Both the keys and the values you store are treated as .NET objects.
A key update came with PowerShell version 3.0, which introduced the ability to create ordered dictionaries. The main difference between a regular Hashtable and an ordered dictionary is how they handle the order of your keys. In an ordered dictionary, the keys are always kept in the same order you added them. However, with a regular Hashtable, the order of the keys is not guaranteed.
Syntax
The following statement is the syntax to create the Hashtable:
$variable_name = @{ <key1> = <value1> ; < key2> = <value2> ; ….. ; < keyN> = <valueN>;}
The following statement is the syntax to create an ordered dictionary:
$variable_name = [ordered] @{ < key1> = <value1> ; < key2> = <value2> ; ….. ; < keyN> = <valueN>;}
Create a Hash Table
The following are the steps to create the hash table in the PowerShell:
- Create a hast table with the @ symbol at the beginning.
- Enclose the hash table in the braces.
- Enter one or more key/value pairs as the content of the hash table.
- To separate each value form it’s key, we must use an equal sign (=).
- To separate the key/value pairs, we must use a semi colon (;) or the line break.
- Those keys which contain the spaces enclosed them in a quotation marks. And the values must be valid expression of PowerShell.
- To manage the Hast table, assign it to the variables.
- When you assign the ordered hast table to the variable, you can place the ordered attribute before @ sign.
To create an empty hash table, type the following command in the PowerShell:
$variablename = @{}
We can also add the keys and values to the hash table when we create it.
The following example describe how to create the hash table with three keys and their values.
$student = @{ name = “Abhay” ; Course = “BCA” ; Age= 19 }
Display a Hash table
To display the hash table, type the name of the variable which stores it. By default, it displays the table with two columns. One column is for the keys and another for their values.
The following command displays the result of hash table:
$Student
Output:
Name Value
—- —–
name Abhay
Course BCA
Age 19
To display all the keys or all the values of the hash table, use the dot (.) notation. The following example displays all the keys of the above example:
$Student.keys
Output:
Course
name
Age
The following example displays all the values of the above example:
$Student.values
Output:
Abhay
BCA
19
Hash tables have a “count” property, which indicates the total number of key/value pairs in the hash table. The following command will display the total number of key-value pairs in the above example:
$Student.count
Output:
3
Check out Trending Blogs – Click Here
Visit LinkedIn page – Click Here