Prof. Powershell

Managing Your PowerShell Environment with PSDrive

Here's a breakdown of how to get the most out of the PSDrive.

Last year, I wrote a lesson on working with the ENV: PSDrive. I still encourage students to use $ENV:Computername to get the local computer name instead of using localhost. But I want to make sure you understand about this PSDrive. The values that you see exist only within the current PowerShell session. Any changes you make only apply to the current session.

This means I might have a variable:

PS C:\> $env:PROCESSOR_ARCHITECTURE
AMD64

But I can "remove" it.

PS C:\> del env:PROCESSOR_ARCHITECTURE

I did not use the $ sign because I wanted to delete the entry in the ENV: PSDrive. And now it is gone. However, if I start a new PowerShell session, the variable is back. So changes you make inside PowerShell are not persistent. If you want to make permanent changes, you'll need to actually modify the registry.

System environmental variables are found like this:

PS C:\> get-itemproperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'

User specific environmental variables can be found like this:

PS C:\> get-itemproperty HKCU:\Environment

Right now, my %TEMP% variable is defined thus:

PS C:\> $env:temp
C:\Users\Jeff\AppData\Local\Temp

But I want to change it to a folder on my faster SSD drive, D:\Temp. You can't simply assign a new value in the ENV: PSDrive. I will have to modify the registry.

PS C:\> set-itemproperty HKCU:\Environment -Name TEMP -Value "D:\Temp"

In this particular case, I don't see the new environmental until I reboot.

PS C:\> $env:TEMP
D:\Temp

I could also create new environmental variables.

PS C:\> Set-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name PSVer

[Click on image for larger view.] Figure 1.

Eventually that will show up in my ENV: PSDrive. This is also something I could use in the CMD shell or for other non-PowerShell applications that use environmental variables. Although from a practical perspective, if none of that matters it is just as easy to create a variable in my PowerShell profile.

$PSVer = $psversiontable.psversion.tostring()

The other approach is to use the .NET Framework:

PS C:\> $PSVer = $psversiontable.psversion.tostring()
PS C:\> [environment]::SetEnvironmentVariable("PSVer",$PSVer)

This has an added benefit in that the change is immediate.

If you access to environmental variables, they are very easy to reference in PowerShell. If you think you need new environmental variables, consider how they are going to be used and referenced. Your PowerShell profile script may be the only tool you need.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.