Connect to a network folder with username / password in Powershell

I often access shared network folders in Powershell to capture files, etc. But if a share requires a username / password, Powershell does not ask me for them, unlike Windows Explorer. If I first connect to a folder in Windows Explorer, Powershell will let me connect.

How can I authenticate with Powershell?

+53
powershell
Nov 19 '08 at 19:27
source share
5 answers

At first glance, I really want to use New-PSDrive providing my 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 says that you can provide a PSCredential object PSCredential but if you look closely, the cmdlet does not yet support this. Maybe in the next version, I think.

Therefore, you can use net use or the WScript.Network object by 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

Obviously, in new versions of PowerShell, the New-PSDrive works to map network resources to credentials!

 New-PSDrive -Name P -PSProvider FileSystem -Root \\Server01\Public -Credential user\domain -Persist 
+60
Nov 20 '08 at 15:59
source share

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

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

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

+38
Nov 19 '08 at 20:23
source share

PowerShell 3 now supports this out of the box .

If you are stuck in PowerShell 2, you basically need to use the deprecated net use command (as suggested earlier).

+9
Sep 25 '12 at 16:01
source share

On the same machine, I try to run the same NET USE from a regular command line and from the PowerShell v2 command line:

NET USE Link: SharePoint

In CMD it works fine.

In PS , he says that it works fine, and the drive maps. However, in general, the drive status specified when entering only NET USE is empty, and the drive mapping does not work (i.e. you cannot receive data from the connected drive).

This may be a SharePoint problem, since NET USE from powershell sometimes works, sometimes not, but still it seems that there is a difference between CMD and PS.

+2
Dec 20 '12 at 10:05
source share

I would like to share my experience here.

I often use a network drive to retrieve information from a remote server. If you are using Powershell 2.0 on a remote server.

The best option is to use the NET USE command.

 Net use \\10.10.10.10\SharedDrive "password@123" /User:Abcd.com\username 
0
Sep 23 '17 at 7:01 on
source share



All Articles