Prof. Powershell

One Destination, Many Paths

Here's how to have many different environmental variables in PowerShell all point to the same folder.

Recently on Twitter I posted a quick tip about adding something new to the ENV PSDrive.

new-item env:docs -value (join-path -path $env:USERPROFILE -childpath Documents)

This adds a new environmental variable, but in this session only.

PS C:\> dir env:docs

Name                           Value                           
----                           -----                           
docs                           C:\Users\Jeff\Documents         

But you could use it like this:

PS C:\> dir $env:docs

My original intent was merely to show how to add a temporary environmental variable, should you want to. I also give a useful example of how to use Join-Path which is much better than concatenating strings. Depending on your needs you could also create a variable or even a PSDrive.

PS C:\> $docs = (join-path -path $env:USERPROFILE -childpath Documents)
PS C:\> new-psdrive -Name Docs -PSProvider FileSystem -Root (join-path -path $env:USERPROFILE -childpath Documents)

But as with many things in PowerShell, there is more than one path to the final destination. As several people pointed out the Documents folder could be pointed to a different location, which is true. Even though I was really intending with my tweet to show something with New-Item, let’s look at other options for the value.

The MyDocuments is considered a special folder and Windows has long had ways of programmatically discovering the path. Back in the days of VBScript, we use the Wscript.Shell object, which is still viable today in PowerShell.

PS C:\> $w = new-object -com "wscript.shell"
PS C:\> $mydocs = $w.SpecialFolders.Item("MyDocuments")
PS C:\> $mydocs
C:\Users\Jeff\Documents

Personally, I don’t have any issue using this approach, although I suppose it isn’t as efficient as using the .NET Framework.

PS C:\> $mydocs = [environment]::GetFolderPath("MyDocuments")

As you can see, there are several paths to get to the same destination. Once you have the information, you also have a number of ways to persist or use it: variable, PSDrive or env: item. It comes down to how you plan on using it. If you simply want a way to easily reference My Documents, then a variable probably makes sense. If you want a shortcut to the folder so you can interact with files then a PSDrive is the right choice. An advantage to setting an environmental variable is that if you launch a CMD session from your PowerShell session, it will inherit your environmental variables.

So, many paths, one destination. Enjoy your journey.

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