Fun with PSDrive Locations

A PowerShell PSDrive is a mapping between a PowerShell provider and a resource. The majority of our work is done in a file system PSDrive that corresponds to a file system drive. Let me show you a little trick that might come in handy with a PSDrive. My “trick” should apply to just about any PSDrive, but I’m going to demonstrate with a file system drive.

First, here’s what one of these drives looks like.

PS C:\> get-psdrive e | format-list

Name            : E
Description     : VM
Provider        : Microsoft.PowerShell.Core\FileSystem
Root            : E:\
CurrentLocation :

Notice the CurrentLocation property. It is blank. When I set my location to this drive, I’ll be at the root.

PS C:\> e:
PS E:\> cd \temp
PS E:\temp>

Now, look at the PSDrive again.

PS E:\temp> get-psdrive e | format-list

Name            : E
Description     : VM
Provider        : Microsoft.PowerShell.Core\FileSystem
Root            : E:\
CurrentLocation : temp

As you would expect, the CurrentLocation property has changed. Now for the cool part. You can change the location “behind the scenes”. This works best if you aren’t in the drive you are going to change. All I need to do is get the PSDrive object and set a new value for the CurrentLocation property. The value is relative to the root of the PSDrive.

PS E:\temp> cd c:\
PS C:\> (get-psdrive e).Currentlocation="\stuff\this\that"

I could have saved the results of the Get-PSDrive expression to a variable and then set the CurrentLocation property, but because I’m doing this interactively in the shell I opted for a shortcut approach. When I change back to E: I am in a new folder.

PS C:\> cd e:
PS E:\stuff\this\that>

Sure, I could have used Set-Location and specified the path. But maybe you want to pre-set a location for a future command. I can see setting a default locations for some PSDrives in your PowerShell profile.

(get-psdrive c).CurrentLocation="\scripts"
(get-psdrive e).CurrentLocation="\temp"
(get-psdrive hklm).CurrentLocation="\Software\Microsoft\Windows\CurrentVersion"
CD S:

In a new shell, these drives automatically have a new default location.

PS S:\> get-psdrive c,e,hklm | format-list

Name            : C
Description     :
Provider        : Microsoft.PowerShell.Core\FileSystem
Root            : C:\
CurrentLocation : \scripts

Name            : E
Description     : VM
Provider        : Microsoft.PowerShell.Core\FileSystem
Root            : E:\
CurrentLocation : \temp

Name            : HKLM
Description     : The configuration settings for the local machine.
Provider        : Microsoft.PowerShell.Core\Registry
Root            : HKEY_LOCAL_MACHINE
CurrentLocation : \Software\Microsoft\Windows\CurrentVersion

PS S:\> cd hklm:
PS HKLM:\Software\Microsoft\Windows\CurrentVersion>

This gives me defaults that mean less typing, but I haven’t given up the ability to navigate elsewhere in the PSDrive.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati


About the Author

PowerShell.org Announcer

This is the official account for PowerShell.org and sponsor announcements.