PowerShell 3's PSDrive Persistence
New to PowerShell 3: PSDrives that no longer disappear when you end a session. Now, this week's nifty trick.
- By Jeffery Hicks
One of the frustrations for many PowerShell beginners is how PSDrives correlate to drives that are visible in Windows Explorer. In PowerShell 2, you could add a new PSDrive, say to a network share, but it wouldn't be seen outside of your new PowerShell session. In addition, when your PowerShell session ended, so did the drive "mapping." If you wanted it in another a PowerShell session you needed to re-create it. Fortunately, there are some nice enhancements around PSDrives in PowerShell 3.
The first major enhancement is that you can now make new PSDrives persistent:
PS C:\> New-PSDrive -Name I -PSProvider FileSystem -Root \\chi-fp01\it -Persist -Credential globomantics\jeff
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
I 18.43 1.47 FileSystem \\chi-fp01\it
This command creates a new PSDrive mapped to a network share using alternate credentials. By using -Persist, I also get a drive mapping in Windows. In essence using -Persist achieves the same result as using the legacy Net Use command. In fact, I can use this command to verify the new drive:
PS C:\> net use i:
Local name I:
Remote name \\chi-fp01\it
Resource type Disk
# Opens 0
# Connections 1
The command completed successfully.
The alternate credentials were also passed to the underlying Net Use command.
Now I have a drive that is accessible in Windows. The PSDrive will persist between PowerShell sessions and it even reboots until I remove it. The one requirement is that you must define the new PSDrive using an available drive letter for the PSDrive name.
Also, don't forget that drive mappings are stored per user. Normally this shouldn't matter much, but be aware that when you run an elevated PowerShell session that is actually using different credentials -- if you map a persistent drive, you may not see it in Windows.
The other enhancement you will appreciate is that when you add or remove external drives, PowerShell will dynamically manage the corresponding PSDrive. This means that if you plug in a USB drive and mount it in Windows with a drive letter, you will get a corresponding PSDrive, even in your open PowerShell session. You do not need to exit or restart. The drive will also be removed dynamically when you disconnect the external drive.
I think most PowerShell users will welcome these changes. As always, be sure to read full help and examples for New-PSDrive.
Jeffery Hicks is a multi-year Microsoft MVP in Windows PowerShell, Microsoft Certified Professional and an IT veteran with almost 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff is a regular contributor to a variety on online sites, as well as frequent speaker at technology conferences and user groups. Keep up with Jeff and his projects at http://jdhitsolutions.com/blog.