Over the last few articles I’ve been discussing working with locations and navigating in the PowerShell console. In the last article I made a reference to a PSDrive, and even created a new one. Let’s look at this in a bit more detail.

A PSDrive is a location to a data structure that is managed by a piece of software called a provider. The provider translates the actions of a common cmdlet like Get-ChildItem (dir) into something that the provider understands. In addition to the file system provider, you have providers for the registry, function, the certificate store and many more depending on modules you load or software you install.

This means you can use, for the most part, the same commands to navigate the registry as you do the file system. If you run the command, Get-PSDrive, you can see all of the drives in your session. Many, if not most, of these drives are created automatically whenever you start PowerShell. But you can easily create additional PSDrives to simplify things.

Let’s look at the file system since I’m assuming this is a place you spend a lot of time. To create a new PSDrive you need to provide a drive name, the provider and the location or root. For the sake of illustration I took a folder I frequently use, C:\work\dev\scripts. I don’t want to have to constantly type the path whenever I want to change to it. So I can create a PSDrive to serve as a shortcut.

PS C:\> New-PSDrive -Name W -PSProvider FileSystem -Root C:\work\dev\scripts

Create a PSDrive

Create a PSDrive

To use, all I need to do is change locations to the new W drive.

PS C:\> cd w:
PS W:\>

Remember that the PSDrive name does not include the colon (:) but you need to include it when using the drive to set location. But as you saw in my last article, you are not limited to drive letters, no are you limited to local folders.

PS C:\> new-psdrive PSH filesystem \\jdh-nvnas\files\powershell

The parameters are positional, but I just created a drive called PSH that is mapped to a UNC with some network PowerShell resources. I can use the drive like any other drive.

PS C:\> dir psh: | measure length -sum

Count    : 79
Average  :
Sum      : 657723660
Maximum  :
Minimum  :
Property : length

If necessary, you can also provide alternate credentials.

PS C:\> new-psdrive DC01 filesystem \\chi-dc01\c$ -Credential globomantics\administrator

This command creates a drive mapped to the C drive on one of my domain controllers using an alternate credential.

Mapped drive in PowerShell

Mapped drive in PowerShell

Like most items you create in PowerShell, these PSDrives only exist for as long as your PowerShell session is open. When I close this session, these drives will go away. They are also not visible to other PowerShell sessions I might have running such as the PowerShell ISE. In many cases, the solution is to put the New-PSDrive command into your profile script.

However, PowerShell 3 introduced a new feature aimed at mapped drives to make them persistent. The one caveat is that your drive name must be a valid drive letter.

PS C:\> new-psdrive Y filesystem \\chi-dc02\c$ -Credential globomantics\administrator –Persist

Using the –Persist parameter in essence maps the drive in Windows which you can see with the legacy NET USE command.

PS C:\> net use y:
Local name        Y:
Remote name       \\chi-dc02\c$
Resource type     Disk
Status            OK
# Opens           0
# Connections     1
The command completed successfully.

Although there is one other gotcha I’ve run into with this. If you make a persistent drive in an elevated session, you will only see it in another elevated session. But if you are always in an elevated session that shouldn’t be that much of a deal. The bottom line is that you can map drives from within PowerShell and always have them available.

I love being able to create shortcuts to long paths and use more descriptive names. I have a number of PSDrives defined in my profile.

List PSDrives

List PSDrives

These drives make me more efficient and make PowerShell even easier to use. And even though I’ve been demonstrating with the file system provider, the same concepts and techniques apply to other providers like the Registry, IIS or ActiveDirectory. Oh, and to remove a PSDrive it doesn’t get any easier than this:

PS C:\> remove-psdrive y

Just make sure you aren’t in the drive you are trying to remove.

What kind of shortcut drives do you use or think would be helpful?

2 Comments
  1. JM 7 years ago

    Very nice!  Hard to find good info on obscure PowerShell commands like this!

  2. Emraan 4 years ago

    Truly made my day. Have been trying to write a remote script that involves Copying Files to my servers. Enabling Double Hop for PS requires some work and was looking for a simple solution. PS Drive solved it and made referring Fileshares a piece of cake. Appreciate your effort in the article.

Leave a reply

Please enclose code in pre tags

Your email address will not be published.

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account