Using PowerShell Drives as Folder Shortcuts

Adam Bertram

Adam Bertram

Read more posts by this author.

One of the best parts of going to conferences is discovering a lot of little tips and tricks that the presenters don’t necessarily mean to teach. It’s amazing how many little tips I’ve picked up over the years from various presenters that didn’t even know they were teaching me something new!

This latest tip came from Kirk Munro at the PowerShell Summit 2016. Kirk was casually browsing through some files to get to the next demo when I noticed he was referencing PS drives in a non-standard way. Instead of C:\ or D:\, he was referencing files like Modules:\ and Scripts:\ which I thought was interesting. He briefly mentioned these were simply PS drives pointed to various folders on his system. Genius!

I’ve got lots of common folders I need to access on my system this would be great for so I’ve implemented this little trick and it’s been saving me a few seconds every time I need to access these folders (which is often).

To do this requires creating a few new PS drives with the New-PSDrive cmdlet and is so simple I’m not sure why I never thought of it. If you’re not familiar with a PS drive, it’s essentially a “bucket” that a PS provider gives to you. For example, there’s the FileSystem PSprovider that we’ll be using, the Cert provider to browse installed certificates or the Registry which allows you to browse various keys and values. Each kind of item like a certificate, registry key, whatever can be referenced just like you’re used to on the file system like C:\ or D:\.

In our case, we’ll be using the FileSystem provider to build new PS drives to act as shortcuts to other folders. To do this, we’ll provide New-PSDrive with a few parameters and that’ll be it! We’ll simply need to fill in the Name, Root and specify a PSProvider we’ll be using like this:

$null = New-PSDrive -Name Modules -Root 'C:\Program Files\WindowsPowerShell\Modules' -PSProvider FileSystem

If I’m constantly having to type in that full path every time to access my PowerShell modules, I’m now spared. I can simply reference them by using Modules:.

Since New-PSDrive does not persist across sessions, I recommend placing them in your PowerShell profile so they’re mapped every time you being a new console session.

I use this technique to dynamically create branch folders. I’m always modifying different branches of a software project and have a folder for each particular branch inside of a single parent folder. I can now access each of my branches as a PSDrive since I’m enumerating each folder in my project parent folder and creating a PSDrive for each of them.

@(Get-ChildItem -Path $parentFolder).where({$_.PSIsContainer}).foreach({
    $null = New-PSDrive -Name $_ -Root "$($_.FullName)\Modules" -PSProvider FileSystem

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!