Eddie Groves
Eddie Groves

Reputation: 34888

Connecting to a network folder with username/password in Powershell

I often access shared network folders in Powershell to grab files etc. But if the share requires a username/password, Powershell does not prompt me for these, unlike Windows Explorer. If I connect to the folder first in Windows Explorer, Powershell will then allow me to connect.

How can I authenticate myself in Powershell?

Upvotes: 69

Views: 316974

Answers (4)

Scott Saad
Scott Saad

Reputation: 18372

Back in Windows PowerShell 2, this was a problem. When you supplied credentials to the old New-PSDrive ...

> New-PSDrive -Name P -PSProvider FileSystem -Root \\server\share -Credential domain\user

It failed!

New-PSDrive : Cannot retrieve the dynamic parameters for the cmdlet. Dynamic parameters for NewDrive cannot be retrieved for the 'FileSystem' provider. The provider does not support the use of credentials. Please perform the operation again without specifying credentials.

Your best bet was to use net use or the WScript.Network object, calling the MapNetworkDrive function:

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("u:", "\\server\share", $false, "domain\user", "password")

Modern PowerShell

Since PowerShell 3 (in Windows 8 and Server 2012), the New-PSDrive cmdlet now supports the -Credential parameter properly, and has a -Persist flag that makes these stick just like the other mechanisms.

If you call it with just a user name, it will prompt securely but interactively for the password:

New-PSDrive -Name P -PSProvider FileSystem -Root \\Server01\Public -Credential user\domain -Persist

Or you can pre-create credentials, if you have the password in a variable or text file:

$Password = ConvertTo-SecureString (Get-Content mypassword.txt) -AsPlainText
$Credential = [PSCredential]::new("user\domain", $Password)

New-PSDrive -Name P -PSProvider FileSystem -Root \\Server01\Public -Credential $Credential -Persist

Upvotes: 80

Jaykul
Jaykul

Reputation: 15824

Since at least 2012, PowerShell 3 and up support this out of the box by specifying the -Credential parameter.

If you simply provide the user name, you'll be prompted interactively but securely for the password:

New-PSDrive -Name J -PSProvider FileSystem -Root \\myserver\myshare -Credential mydomain\myname -Persist

If you need to pre-create the credential, you could, for example, read the password from a file like this:

$Credential = [PSCredential]::new("MyDomain\MyUserName", (ConvertTo-SecureString (Get-Content myPassword.txt) -AsPlainText))

New-PSDrive -Name J -PSProvider FileSystem -Root \\myserver\myshare -Credential $Credential -Persist

Upvotes: 9

Stoffi
Stoffi

Reputation: 764

You can use the New-SmbMapping CMDlet.

New-SmbMapping -RemotePath '\\server\path' -UserName 'server\user' -Password 'passwort'

You don't have to use the -LocalPath parameter, so it does not mount it as a drive. It just establishes the connection to the share.

Upvotes: 6

Anon
Anon

Reputation:

This is not a PowerShell-specific answer, but you could authenticate against the share using "NET USE" first:

net use \\server\share /user:<domain\username> <password>

And then do whatever you need to do in PowerShell...

Upvotes: 58

Related Questions