Thursday, December 16, 2010

Map Network Drive with a users credentials


Three quick and easy ways to Map a UNC path to a local drive. The samples below (DOS, VBS, PowerShell) map the Q: drive to a network path using different credentials. you can also remove the parameters for credentials and it will create the connection with the current logged-on user.

DOS
NET USE Q: \\MyUnc\Path /USER:MyUser MyPassword

VBS

strDrive = "Q:"
strUNC = "\\MyUnc\Path"
strProfile = "False"   ' Mapping (not) stored in user Profile
strUser = "MyUser"
strPassword = "MyPassword"
Set objNetwork = CreateObject("WScript.Network") 
objNetwork.MapNetworkDrive strDrive, strUNC, strProfile, strUser, strPassword 

Powershell

$Drive = "Q:"
$UNC = "\\MyUnc\Path"
$Profile = "False"
$User = "MyUser"
$Password = "MyPassord"
$Network = New-Object -com WScript.Network; 
$Network.mapnetworkdrive($Drive,$Unc, $Profile, $User, $Password)

The code above is basically the same as done in VBS, You can also make the DOS call in Powershell.

No comments: