Introduction to Microsoft PowerShell’s Drives
Learning how to map a network drive in PowerShell is much easier than the equivalent MapNetworkDrive in VBScript.
My biggest surprise with Get-PSDrive was that the registry appeared in the results. I was expecting a list of the local and network drive, but I had not anticipated HKLM or ENV amongst the catalog of drives available to PowerShell.
Topics for Microsoft PowerShell’s Drives
- PowerShell MapNetworkDrive
- Childitem (or Get-Childitem)
- Summary of PowerShell’s Drives
I have to confess that at first I was unable to map a network drive. However, eventually I hit upon this indirect method:
Success Experiment 1
# PowerShell Map Network Drive
$Net = $(New-Object -ComObject WScript.Network)
Note 1: The drive letter is "S:" and the Network Share in called ‘ezine’ on a computer called ‘grand’.
Note 2: MapNetworkDrive is a PowerShell method applied to the comObject.
(Failed Experiment 1)
1) Mapped a network drive to x: (\\grand\scripts)
2) Get-PSDrive x | format-List
3) Nowhere in the property list could I see reference to (\\grand\scripts) or anything similar
(Failed Experiment 2)
1) I discovered that you could create a new drive so I tried
2) New-PSDrive y
3) Provider FileSystem (PowerShell asked for Provider)
4) Root Y:\ (PowerShell asked for Root)
In PowerShell, the command Drive revealed the new drive, however the letter Y:\ did not appear in Explorer.
Success Experiment 2
# PowerShell Map Network Drive
New-PSDrive -name GuyScripts -psprovider filesystem -root d:\ scripts
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.
As anticipated Get-PSDrive (note singular noun) displays hard drives such as C:\ and D:\. Here are the extra namespaces that I was not expecting:
Env (Environmental Variables e.g. HomeDrive, UserName)
To list or investigate the following three ‘file systems’ you need the get verb, type:
Here is an utility where you can review firewall settings such as access control lists (ACL), or troubleshoot problems with network address translation (NAT).
Other reasons to download this SolarWinds Firewall Browser include managing requests to change your firewall settings, testing firewall rules before you go live, and querying settings with the browser’s powerful search options.
Guy recommends that you download a copy of the SolarWinds free Firewall Browser.
Get-PSDrive and the Registry
Remember that to access the registry (HKLM) or Env you need the ‘set’ verb
Set-location HKLM: (Note the colon)
Set-location Env: (Note the colon)
One interesting command is Get-PSDrive name. Where name is HKLM, Variable or Alias.
In the context of a drive, PowerShell uses Get-Childitem to list the contents. You may wish to use dir, or you may prefer to leave the old DOS commands at the PowerShell door and learn the new style Verb-Noun pairs. Incidentally, it is worth remembering that all PowerShell nouns are singular, Microsoft is determined to enforce consistency wherever possible.
A handy command is set-location. Microsoft include a mapping for many old dos commands, therefore if you prefer, use the alias cd, rather than the modern set-location.
The Get-PSDrive command returned more items than I expected. In amongst the usual C:\ and D:\ drive letters was HKCU and Alias. By creating a new com object it’s possible to MapNetworkDrive just as you would with VBScript.
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.