68

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?

Eddie Groves
  • 33,851
  • 14
  • 47
  • 48

4 Answers4

79

At first glance one really wants to use New-PSDrive supplying it credentials.

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

Fails!

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.

The documentation states that you can provide a PSCredential object but if you look closer the cmdlet does not support this yet. Maybe in the next version I guess.

Therefore you can either 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")

Edit for New-PSDrive in PowerShell 3.0

Apparently with newer versions of PowerShell, the New-PSDrive cmdlet works to map network shares with credentials!

New-PSDrive -Name P -PSProvider FileSystem -Root \\Server01\Public -Credential user\domain -Persist
Clijsters
  • 4,031
  • 1
  • 27
  • 37
Scott Saad
  • 17,962
  • 11
  • 63
  • 84
  • 2
    There's an open bug for this: https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=334084&SiteID=99 – halr9000 Nov 20 '08 at 21:21
  • 1
    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. – Don Jones Nov 29 '08 at 17:22
  • 3
    Note that [as of Powershell 3.0](http://technet.microsoft.com/en-us/library/hh849829.aspx), 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. – Bacon Bits Dec 15 '14 at 14:14
  • 1
    @BaconBits. Thank you for letting me know on this! I've updated the question to reflect the 3.0 version of PowerShell. – Scott Saad Dec 17 '14 at 05:01
  • 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. – StingyJack Aug 25 '18 at 13:53
  • All this answer are binding it to an drive. Which is not requested in the question. The net use answer push the use of UNC Path, which should work with PS too – JackGrinningCat Feb 22 '19 at 18:24
56

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

  • 2
    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. :) – halr9000 Nov 20 '08 at 15:28
  • This is good way to access windows share from other languages. – Trismegistos Jan 16 '14 at 16:17
  • I still haven't found an other way in powershell to do this: net use \\server\ipc$ /user: password -> cd \\server\share – JDC May 06 '19 at 10:40
  • You can use this method with conjunction of ``` $networkCreds = $creds.GetNetworkCredential() $username = "$($networkCreds.Domain)\$($networkCreds.UserName)" $password = $networkCreds.Password ``` – Dasith Wijes Apr 23 '20 at 06:40
8

PowerShell 3 supports this out of the box now.

If you're stuck on PowerShell 2, you basically have to use the legacy net use command (as suggested earlier).

Jaykul
  • 15,370
  • 8
  • 61
  • 70
1

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.

Stoffi
  • 595
  • 6
  • 9