Variables in powershell are two types.

  • User defined variables
  • Environment variables Variables are used to store the value temporarary, hat internally the store the value in a memory location.

You can declare the variable once, access the variable in multiple places

Variables names are prefixed with $ followed by an characeters. It contains an value assigned to it using #

example

$variable1=10
$variable2=11
$variable3=testdata

variables also used to store the result of a command. Command can be any operation or action. Example are arthematic operations.

$variable1=10
$variable2=11
$variable3=$variale1+$variable2

Variables stores the data of different data types.

Let’s discuss about different data types supported in Powershell.

Declare Multiple variables with a single line

You can declare multiple variables in a single line with data assignment

For example, declare three variables, assigned with values in comma separated variables and data.

$first, $second,$third=1,2,3
write-host"$first $second, $third"

Variable Scopes

In Powershell, Variables or function names can have a scopes

  • Global : variable avilabile and accessed globally.
  • Local : Local variable where it is defined
  • Script: scope of the variable exists in current script only.

To set the variable in a scope, use below syntax

$scope:variable=value: - set the variable with a value in given scope $scope:variable: access the variable in a given scope

Power shell assigns two more scopes when you defina an variable in a scope

Data types in Powershell

Datatype allows you to store different values for an variable. The following are supported types in Powershell.

  • Integer: Represents the whole numbers, example are 1,90
  • Float: Contains precission and decimal numbers, Example are 1.11
  • String: Contains characters and letters, Example are content
  • Boolean: Boolean value such as True or False
  • DateTime: represents date and time stamp
  • arrays ; Collection of elements stored under a single variable
  • hashtable: Stores the key and value pair elements

Powershell Environment variables

Powershell provides $env scope that stores all environment varibles.

  • Read Environment variables

TO read single environment variable

$env:windir

windir is an environment variablem prints windows directory.

You can also read all environment variables and print using below command lin script file.

Get-ChildItem env:

It displays environment variable name and value

Name                           Value
----                           -----
_MSYS2_BASH                    C:\tools\msys64\usr\bin\bash.exe
_MSYS2_PREFIX                  x86_64
ALLUSERSPROFILE                C:\ProgramData
ANDROID_HOME                   A:\android\sdk
ANT_HOME                       A:\Java\apache-ant-1.10.0

Another way using dir Env: command in powershell list out all environment variables

dir Env:
  • To set environment varlable
# Add environment variable
$env:My_HOME = "welcome"

It adds My_HOME variable with value to environment.

Prints the value of My_HOME

Get-ChildItem Env:My_HOME
Name                           Value
----                           -----
My_HOME                        welcome

another way, print welcome using $env:My_HOME command

  • To remove environment variable Remove-Item is used to remove the environment variable
Remove-Item -Path Env:My_HOME

When you add or modify an environment variable via script or a command, life the variable only exists on same session