Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Fun with PSDrive Locations

Posted on July 10, 2012

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d