Prof. Powershell

Environmentally Friendly

Powershell has its own variables, but you can also tap Windows' environment variables within your scripts

If you have any experience with traditional batch files in the CMD shell, you may used environmental variables like %username%. These variables are likely to be available everywhere making your script more portable. Even though a PowerShell script typically uses its own variables, you can still access the environmental variables.

The Environment PSDrive provider creates a PSDrive called ENV. If you do a directory of ENV: you should see the same environmental variables that you see when running SET in a CMD shell. These variables are completely separate from their CMD counterpart. You can delete, modify or create new variables in the ENV PSDrive and it will have no effect on Windows.

If you need to change an environmental variable that will affect the rest of your CMD and Windows sessions, you'll need to edit the registry (but let's save that topic for a future lesson).

More likely, you'll want to use the environmental variables in your script. To retrieve a value use the environmental variable name prefixed by $env: like this:

PS C:\> $env:username
Jeff

The $ character acts as a delimiter that gives us only the requested item in the ENV PSDrive. Let's check out some other examples that use environmental variables.

Perhaps you want to find out the size of all files in %temp%:

PS C:\> (dir $env:temp -recurse | measure-object -sum length).sum/1MB
2.91187381744385

The Get-ChildItem cmdlet (I'm using the DIR alias) returns all files in the %temp% directory and pipes them to Measure-Object, which calculates the sum of all the files' lengths (size) property. The default value is in bytes, but I'm dividing it by 1MB to return a more meaningful number.

Here's one more:

PS C:\> $(([ADSI]"WinNT://$env:computername/administrator,user").passwordage)/86400 -as [int]
51

This one- liner uses ADSI to retrieve the local administrator account from the local computer. It then takes the PasswordAge property and divides it by 86400 to return the password age in days. I cast the value as an integer [int] merely to round it off. And yes, I know I could have used localhost in this expression, but sometimes it won't work in other ADSI expressions. That's why I've started using the "real" computername for consistency.

As your PowerShell experience grows, I'm sure you’ll find other ways to take advantage of these environmental variables.

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
Most   Popular

Upcoming Training Events

0 AM
Live! 360 Orlando
November 17-22, 2024
TechMentor @ Microsoft HQ
August 11-15, 2025