PowerShell New-PSDrive

PowerShell's Way of Mapping a Network DriveNew-PSDrive

The modern method for mapping a network drive is to use New-PSDrive.  PowerShell 3.0 introduces the -Persist parameter so that we can not only see the name of your new drive in Windows explorer, but also know it's still there the next time we logon.

In addition to mapping a letter to \\Server\Share you can connect to local drives, and even registry hives.

Topics for Windows PowerShell New-PSDrive

 ♣

Get-Help New-PSDrive

Calling for PowerShell's built-in Get-Help is particularly fruitful with New-PSDrive, this is because it shows that there is not one, but three required parameters.

# Get started with New-PSDrive
Get-Help Get-PSDrive

-Name <String>
Specifies a name for the new drive. For persistent mapped network drives, type a drive letter. For temporary drives type you are not limited to drive letters.
Required? true
Position 1

PSProvider <String>
Specifies the Windows PowerShell provider, for example, FileType or Registry.
Required? true
Position? 2

-Root <String>
Specifies the data store location, for example, \\Server\Drivers, or a registry key such as HKLM:\Software\Microsoft\Windows NT\CurrentVersion.
Required? true
Position? 3

New-PSDrive: Three Required Parameters

Here is example illustrating how to use the 3 required parameters: -Name, -PSProvider and -Root (N.B. please change my values). The point of this script is to create a new drive called X:, which is mapped to a network share.

# PowerShell script to create an X: drive
$Network = "\\Server\ShareName"
New-PSDrive -Name X -PSProvider FileSystem -Root $Network

Note 1: I created the $Network variable to encourage you to think of a share name on your network.

Note 2: I tried omitting -PSProvider and its value.  I should have known better! Because PowerShell requires this parameter, my amended script stalled.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Fancy Script to Map a Network Drive with New-PSDrive

Here is a handy script for experimenting with New-PSDrive, especially if you have PowerShell 3.0 and want to introduce the -Persist parameter.

# PowerShell 3.0 creates a new persistent network drive
Clear-Host
$Network = "\\Server\ShareName"
If ((Get-PSDrive [a-z]) -Match 'X') {
Remove-PSDrive X; "Removed X: drive first"
}
New-PSDrive -Name X -PSProvider FileSystem -Root $Network -Persist | `
Format-Table Name, DisplayRoot, Root -AutoSize
Start-Sleep 8
Invoke-Item X:

Note 3: Without the -Persist parameter you will only see this newly created mapped drive in PowerShell; add this parameter and your newly created drive letter will show in Windows Explorer.

Note 4: If you use -Persist, then it's best to include the DisplayRoot property in Format-Table output.

Note 5: Observe the neat use of Invoke-Item.

Note 6: This script features a sister cmdlet Remove-PSDrive.  Incidentally PowerShell never uses the -Delete parameter in cmdlets, for consistency, it always uses the -Remove verb.

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Mapping Network Drives with PowerShell

For me, mapping network drives started with VBScript, indeed, it was the desire to program drive letters connected to network shares that sparked my interest in scripting.  We can map drives in PowerShell v 1.0 by mimicking VBScript and creating a ComObject, but here in version 3, we can get the job done much easier thanks to New-PSDrive and its -Persist parameter.

Create Extra Registry 'Drives' with New-PSDrive

Let us employ New-PSDrive to create a shortcut to the CurrentVersion key in the registry.

# Create a PSDrive to the CurrentVersion key in the registry
Clear-Host
$Reg ="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
New-PSDrive -Name GuyWin -PSProvider Registry -Root $Reg

Note 7: While I have explicitly used the names of the three required parameter, because they are in the correct sequence, you could omit -Name, -PSProvider and -Root, and the script would still work.

Fancy PowerShell Script for a New Registry Drive

Once again, I have created an extended script with neat additions which I hope will give you ideas for your own scripts.

# Create a direct route to the CurrentVersion key in the registry
Clear-Host
$Reg ="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
If ((Get-PSDrive -PSProvider Registry) -Match 'GuyWin') {
Remove-PSDrive GuyWin; "Removed Old GuyWin"
}
New-PSDrive -Name GuyWin -PSProvider Registry -Root $Reg | Out-Null
Get-PSProvider -PSProvider Registry | Select-Object -ExpandProperty drives |`
Format-Table Name, root -AutoSize

Note 8: As with my previous fancy script, the 'If' statement is to cater for us running the script a second time.

Note 9: The final two lines are solely to show what we have created.

SolarWinds Response Time Viewer for WiresharkGuy Recommends: Response Time Viewer for Wireshark

Here is a free tool to troubleshoot network connection and latency problems.  Key concept: this is a free tool from SolarWinds that analyzes network packets captured by Wireshark (also a free tool).

When you inspect the data in the Response Time Dashboard, if you hover over an application such as Teredo or TCP, then you get an orange box showing a breakdown of network and application response times, note the 'Peak value' in addition to the 'Average'.

Download your free trial of SolarWinds Response Time Viewer for Wireshark

Get-PSDrive and Get-Member

Just a reminder that, as with any PowerShell cmdlet, you can pipe New-PSDrive into Get-Member, and thus learn more about the properties and methods that Microsoft created for this command.

Clear-Host
$Network = "\\Server\ShareName"
If ((Get-PSDrive [a-z]) -Match 'X') {
Remove-PSDrive X; "Removed X"
}
New-PSDrive -Name X -PSProvider FileSystem -Root $Network | Get-Member

Note 10: Every time I run Get-Member (GM) I discover a new property or method, in this case its 'Credential', so handy for controlling permissions when creating the new drive letter.

Other Members of the PSDrive Family

We have already met the other members of the PSDrive family:

Remove-PSDrive – Handy for testing scripts, or deleting unwanted mapped network drives.

Get-PSDrive – Reveals PowerShell's thinking by listing drives such as Variable, Function and Alias.  When scripting with Get-ChildItem do remember those colons!  Get-ChildItem Alias:

See more on Get-PSDrive ยป

Summary of PowerShell 3.0 New-PSDrive

The primary job of the New-PSDrive cmdlet is to map network drives, especially now that PowerShell v 3.0 has introduced the -Persist parameter.  However, this is a particularly fruitful cmdlet to learn more about different aspects of PowerShell.  Firstly, observe how PowerShell views the registry, variables and environment as 'drives'.  Secondly New-PSDrive is a classic command to learn about required and positional parameters.

If you like this page then please share it with your friends

 


See more Windows PowerShell  examples of variables

Syntax   • PowerShell Variables   • Get-PSProvider   • PowerShell Env:Path  • Free WMI Monitor

PowerShell Functions   • Get-PSDrive   • PowerShell New-PSDrive   • Remove-PSDrive

PowerShell Home   • PowerShell Environmental Variable   • PowerShell Dollar Variable

Please email me if you have a better example script.  Also please report any factual mistakes, grammatical errors or broken links, I will be happy to  correct the fault.