tags:

views:

5353

answers:

2

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  A: 

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

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
+4  A: 

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

> New-PSDrive -name myShare -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")
Scott Saad
halr9000
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
Looking at the docs, you'll notice that it says the parameter is not implemented for the cmdlet. http://technet.microsoft.com/en-us/library/bb978543.aspx
Scott Saad