Author Kamal Kumar on August 10, 2020 74  Views

PowerShell for Windows and Linux ~ Chapter 2 | Variables in PowerShell

In this chapter we're going to understand variables in PowerShell. How to define variables, use them, type of variables we can define in PowerShell. We'll be covering all of them in very simple and short article
What we will cover in this article -

How to declare variables in PowerShell

 

First method (Using Set-Variable) - 

PS /home/kamal> Set-Variable -Name FirstName -Value 'Kamal Kumar' 
PS /home/kamal> Get-Variable -Name FirstName                     

Name                           Value
----                           -----
FirstName                      Kamal Kumar

PS /home/kamal>

 

Second method (Using dollar sign $) - 

PS /home/kamal> $LastName = 'Kumar'
PS /home/kamal> $LastName 
Kumar

PS /home/kamal>

 

Using variable in a script

 

In last article we've created a script as follow -

# Asking user for input
[int]$num1 = Read-Host "Enter 1st number"
[int]$num2 = Read-Host "Enter 2nd number"

# Doing math
$result = $num1 + $num2

# Displaying result
Write-Host "The Sum of $num1 and $num2 is $result" 

We only need to use variable name with $ sign. And the value will be replaced by value of variable. 

 

Example:

PS /home/kamal> $UserName = "Kamal Kumar"
PS /home/kamal> $Website = "www.techaware.in"
PS /home/kamal> $email_id = "[email protected]"
PS /home/kamal> Write-Host " Hello $UserName, `n We have sent you a email on $email_id related to your article on $Website."       

Hello Kamal Kumar,
We have sent you a email on [email protected] related to your article on www.techaware.in.

PS /home/kamal>

You may have noticed few things, let's discuss.

  1. Write-Host is used to print string on PowerShell console.
  2. `n is used to add new line. Similarly you can use `t for a tab and more. Basically ` is an escape character used to represent next character is not port of string. In our case n and t are not part of string

 

Type of variables

 

  1. Automatic Variables: 
    Automatic variables store the state of Windows PowerShell.
    These variables are created by Windows PowerShell, and Windows PowerShell can change their values as required to maintain their accuracy. Users cannot change the value of these variables. 
    For e.g., the $PSHome variable stores the path to the Windows PowerShell installation directory.
  2. Preference Variables: 
    Preference variables store user preferences for Windows PowerShell. These variables are created by Windows PowerShell and are populated with default values. Users can change the values of these variables. 
    For e.g., the $MaximumHistoryCount variable determines the maximum number of entries in the session history.
  3. User-defined Variables: 
    User-defined variables are created and maintained by the user. By default, the variables that you create at the Windows PowerShell command line exist only while the Windows PowerShell window is open, and they are lost when you close the window. To save a variable, add it to your Windows PowerShell profile. You can also create variables in scripts with global, script, or local scope.