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

7

24

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?

Platonic answered 7/10, 2009 at 10:23 Comment(0)
A
41
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("r:", "\\romeobox\files", $false, "domain\user", "password")

Should do the trick,

Kindness,

Dan

Almuce answered 7/10, 2009 at 10:30 Comment(3)
Out of curiosity, what is the purpose of $false, presumably a return value? Looked it up but can't find the documentation for MapNetworkDrive...Utah
From the documentation persistent: True/False - store the mapping persistently in the users profile. default = falseAnthony
would this work for mapping onedrive locally? with that 'domain/user' parameter, what should domain be?Audre
D
15

Came here looking for how to map drives using PowerShell?

There's a simpler way with PowerShell3.0. New-PSDrive has been updated with the -persist option. E.g.

New-PSDrive -Name U -PSProvider FileSystem -Root \\yourserver\your\folder -Credential yourdomain\username -Persist

In the past, New-PSDrive affected only the current PowerShell session. -persist causes the mapping to be registered with the O/S, as it were. See New-PSDrive

To answer the original question, you can vary the credentials used. Using -Credential to vary the domain\username causes Windows to prompt for a password. Another alternative is to pass a PSCredential object as in the example below. See Get-Credential for more detail.

PS C:\> $User = "mydomain\username"
PS C:\> $PWord = ConvertTo-SecureString -String "mypassword" -AsPlainText -Force
PS C:\> $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
PS C:\> New-PSDrive -Name U -PSProvider FileSystem -Root \\domain\some\folder -Credential $Credential -Persist  
Durwood answered 22/11, 2012 at 12:54 Comment(1)
I think there may be an easier way to obtain the credentials now: $Credential = Get-Credential -Credential "domain\user". This will prompt for a password with the username field already populated.Indention
N
14

If you need a way to store the password without putting it in plain text in your script or a data file, you can use the DPAPI to protect the password so you can store it safely in a file and retrieve it later as plain text e.g.:

# Stick password into DPAPI storage once - accessible only by current user
Add-Type -assembly System.Security
$passwordBytes = [System.Text.Encoding]::Unicode.GetBytes("Open Sesame")
$entropy = [byte[]](1,2,3,4,5)
$encrytpedData = [System.Security.Cryptography.ProtectedData]::Protect( `
                       $passwordBytes, $entropy, 'CurrentUser')
$encrytpedData | Set-Content -enc byte .\password.bin

# Retrieve and decrypted password
$encrytpedData = Get-Content -enc byte .\password.bin
$unencrytpedData = [System.Security.Cryptography.ProtectedData]::Unprotect( `
                       $encrytpedData, $entropy, 'CurrentUser')
$password = [System.Text.Encoding]::Unicode.GetString($unencrytpedData)
$password
Neoclassicism answered 7/10, 2009 at 15:28 Comment(0)
B
2

I found this easy one liner worked in my case (little "out of the box" thinking ;) )

(Start-Process -FilePath "C:\windows\system32\NET.exe" -ArgumentList "USE I: \Server\Volume /USER:XXXXX password /persistent:yes" -Wait -Passthru).ExitCode

And as an added bonus, you get an nice exitcode to report off of. Hope that helps.

Bedspread answered 17/5, 2016 at 12:29 Comment(0)
J
1
$User = "user"
$PWord = ConvertTo-SecureString -String "password" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
$net = $(New-Object -ComObject WScript.Network)
$net.MapNetworkDrive("r:", "\\server\share")
Josephjosepha answered 14/5, 2016 at 20:54 Comment(0)
L
1

If you are set on using powershell native (if there is such a thing), then you can use:

 New-SMBMapping -LocalPath "V:" -RemotePath $NetworkPath -UserName $UserAndDomain -Password $pass 

See: https://learn.microsoft.com/en-us/powershell/module/smbshare/new-smbmapping?view=win10-ps

There are some additional nuggets like Remove-SMBMapping, etc.

Lynching answered 20/5, 2020 at 17:58 Comment(0)
B
0

The domain is "MicrosoftAccount" for email accounts using a PIN for example, so the user value is "MicrosoftAccount\[email protected]" in the PowerShell command.

Balkhash answered 7/7, 2024 at 23:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.