Drive it Home

An exciting feature of PowerShell is that you can present things like the registry as a drive. This is accomplished with the PSDriveInfo object. If you run Get-PSDrive in PowerShell you will see all the “drives” that you can connect to.  You can also create additional drives with New-PSDrive.

The path to My Documents is just too darn long to type everytime I want to change to it. In PowerShell I can simplify this by assigning a drive letter to the path like this:

new-psdrive U -psprovider FileSystem -root “$env:Userprofile\my documents”

Now drive U: is the root of the My Documents folder. This drive mapping is not available outside the scope of my existing PowerShell session. If I open a second PowerShell window, it won’t know anything about drive U. When I close my first PowerShell session and restart, drive U is gone.  I’d have to add this expression to my profile if I always wanted it to be available.  I could also have used something like:
 
new-psdrive Docs -psprovider FileSystem -root “$env:Userprofile\my documents”

and created a drive called Docs. To change to that “directory” I would have to do this:

set-location docs:

The advantage of using a drive letter is that it makes changing drives a little easier.  I also don’t have to worry that any applications or scripts that I start will get confused with a drive letter of docs:.

You can achieve similar results in the CMD world by using SUBST. Any SUBST mappings that you create in CMD.exe will be recognized in PowerShell.