Mapping Network Drives (Part 1)

by Aug 27, 2015

PowerShell supports console commands, so if you need to map a network drive, often the most reliable way is to use good old net.exe like this:

#requires -Version 1
net.exe use M: '\\dc-01\somefolder' /PERSISTENT:YES 

Test-Path -Path M:\

explorer.exe M:\

If you omit "/PERSISTENT:YES", the mapped drive will be temporary and not reconnected once you log off and on again.

Note that you get an error if drive M: is in use already. Replace M: with a star to automatically use the next available drive letter.

To submit logon credentials, use this approach instead:

net.exe use * '\\dc-01\somefolder' /PERSISTENT:YES /USER:training\user03 *

This would authenticate you as training\user03, use the next available drive letter, and interactively ask for the password. Note that this would work in a regular PowerShell console only. It cannot be used in the PowerShell ISE because the PowerShell ISE has no real console and thus cannot interactively ask for a password.

To submit a password, replace the star after the username with the password. This of course is not recommended as this would expose the password to anyone who can look at your code.

Twitter This Tip! ReTweet this Tip!