69

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?

4 Answers 4

80

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
Sign up to request clarification or add additional context in comments.

6 Comments

There's an open bug for this: connect.microsoft.com/feedback/…
Actually, I think the cmdlet supports it just fine - it's the underlying PSDrive provider that isn't doing anything with it. All the *-Item cmdlets have a generic set of behaviors; they just pass them through to the PSDrive provider, though. -filter is another example.
Note that as of Powershell 3.0, you can pass credentials when connecting a drive to UNC paths, and if you use the -Persist option, the drive will be created as a normal Windows mapped drive.
@BaconBits. Thank you for letting me know on this! I've updated the question to reflect the 3.0 version of PowerShell.
In powershell 5.1 (and 6) this still does not appear to work when connecting to a domain computer from a non-domain computer, even when supplying working creds. Like OP, once you open the location in the Root param using windows explorer and supplying creds, you can then New-PSDrive after that.
|
58

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...

4 Comments

This is one of those "use what works" cases where I agree with Anon. Normally, I would go out of my way to provide a PowerShell answer but not this time. :)
This is good way to access windows share from other languages.
I still haven't found an other way in powershell to do this: net use \\server\ipc$ /user:<domain\username> password -> cd \\server\share
You can use this method with conjunction of ``` $networkCreds = $creds.GetNetworkCredential() $username = "$($networkCreds.Domain)\$($networkCreds.UserName)" $password = $networkCreds.Password ```
9

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

3 Comments

Dead link! 1 2 3
Doesn't provide any examples with passwords
There are two examples. The first (as I wrote) will prompt you for the password. The second reads the password from a file ... but you could replace "(Get-Content myPassword.txt)" with "MyPassword" if you just wanted to hardcode the password in-line. However, I would STRONGLY recommend not putting passwords in plain text in a command that's going to end up in your shell and terminal history files, so I won't put it in the examples.
7

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.