configurationpowershellnetworkinghard-drive

Powershell: how to map a network drive with a different username/password


Background: Assume I use the following powershell script from my local machine to automatically map some network drives.

$net = $(New-Object -ComObject WScript.Network);
$net.MapNetworkDrive("p:", "\\papabox\files");

$net = $(New-Object -ComObject WScript.Network);
$net.MapNetworkDrive("q:", "\\quebecbox\files");

## problem -- this one does not work because my username/password
## is different on romeobox
$net = $(New-Object -ComObject WScript.Network);
$net.MapNetworkDrive("r:", "\\romeobox\files");

Question: How do I modify the script so that I can also connect to romeobox, even though my username/password on romeobox is different from that of the other two boxes?


Solution

  • $net = new-object -ComObject WScript.Network
    $net.MapNetworkDrive("r:", "\\romeobox\files", $false, "domain\user", "password")
    

    Should do the trick,

    Kindness,

    Dan