I hope you've been enjoying the last few posts on working with PSDrives. To round out the set, I thought I'd remind you of a command in the PSScriptTools module that I use often, especially when teaching a live class or presenting at a conference. As you've seen, I use PSDrives as shortcuts to commonly used folders. Back in my DOS days, I relied on subst.exe. PSDrives are much easier. Here's my typical dilemma and how I solve it.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Since I tend to be in a PowerShell session for demonstrations, I might have a path in my prompt like C:\work\foo\bar\Lab Demo. Ok, this is a bit artificial but you get the point. The path can take up a big chunk of space and is generally irrelevant. To simplify things, I'd like to have a PSDrive rooted to this location. Of course, I'm too lazy to always type out a New-PSDrive command so I wrote a function called New-PSDriveHere that is part of the PSScriptTools module.
The function will create a PSDrive rooted to the specified path. It defaults to your current location. The name of the PSDrive will be the name of the last part of the current path. If this is a multi-word string, the default behavior is to use the last word.
Or I can use the first word.
The function also has a parameter that will automatically change to the new PSDrive once it is created.
And because I want to type the least amount that I can, there are aliases.
By the way, you can override the naming and specify your own.
These PSDrives will only last for the duration of my PowerShell session. But that is fine because I only need them temporarily. If you have similar needs, install PSScripttools from the PowerShell Gallery. New-PSDriveHere works in both Windows PowerShell and PowerShell 7.