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
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
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
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.
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.
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
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.
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!
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.
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
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
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.
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'.
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.
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:
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
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.