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.
Notice the CurrentLocation property. It is blank. When I set my location to this drive, I’ll be at the root.
Now, look at the PSDrive again.
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.
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.
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.
In a new shell, these drives automatically have a new default location.
This gives me defaults that mean less typing, but I haven’t given up the ability to navigate elsewhere in the PSDrive.