in

PowerShellPowerShell

PowerShell Environment Variable: $env for EveryOne

Use the $Env variable in PowerShell as a directory for manipulating Environment Variables. Learn more about path,username,windir,temp and more.

$Env PowerShell

The Env: is like a variable that contains the variables that have values for ‘Path’ and ‘Windir’.  We can use PowerShell to manipulate the system variables. This is easier than using the GUI to edit the environment variables.  While using the GUI we might have to use the Control Panel to edit the system variables.

PowerShell views the environment as drives.  We can confirm that by using the command given below.

Get-PSDrive

The above entry produces the following results.

get PowerShell drive
get PowerShell drive

The fact that PowerShell views the environment as drives enable us to list the Environment variables like listing the child directories of a drive.

We can use the following command to list the Environment contents sorted based on the name.

Get-Childitem -Path Env:* | Sort-Object Name

$Env: Windir

This is one of the oldest Environment variables.  It is as old as the Windows 95 operating system.  Most of the time, Windir will refer to the C:\Windows.  And if the Windows Operating System is installed on a different partition, Windir will fetch the data of that partition.

$env:windir
Win Dir
Win Dir

The above command will fetch the directory information for the current installation of Windows.

$Env:path

The path variable is the most important variable on a scripting point of view.  We can get the values in the Path variable using the following command.

$env:path
$env:path
$env:path

$Env:temp

Temporary path is obtained by the following command

$env:temp
$env:temp
$env:temp

We can get the temporary path and all the subdirectories for deletion purposes using the below command.  Note that this command will get only all the directories but to delete them we have to combine it with the delete command also.

get-childitem env:temp -recurse
get all the temp directories
get all the temp directories

$Env:username

The user name of the current user is given by:

$env:username

We will get the error if we try to deal with non-alpha numeric characters in the PowerShell terminal.  We will use the curly braces to tackle this problem as shown below.

Use Curly braces with commands:

${env:commonprogramfiles(x86)}
use curly braces
use curly braces

In the image, we can see that entering the command without the {} creates an error.  If we enter the same command with the {} we are able to get the desired output.

Conclusion:

We can get all the Environment variables using the $env variable.  The PowerShell views the environment as directories and so we are able to view its contents as viewing contents of a directory.  It is easy to get the different Environment variable values using this rather than using the GUI.

For more content on PowerShell, stay tuned to TecKangaroo.

Leave a Reply

Your email address will not be published. Required fields are marked *