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?
This is not a PowerShell-specific answer, but you could authenticate against the share using "NET USE" first:
And then do whatever you need to do in PowerShell...
I would like to share my experience here.
I often use network drive to pull information from remote Server. If you are using Powershell 2.0 on remote Server.
Best option is to use NET USE command.
On the same machine, I try the same
NET USE
command from a regular command prompt, and from a PowerShell v2 command prompt:NET USE Link : SharePoint
In CMD
it works just fine.In PS
, it says it works fine, and the drive is mapped. However, mostly the drive's status as listed when you type justNET USE
is blank, and the drive mapping doesn't work (i.e. you cannot get data from the mapped drive).It might be a SharePoint issue as the
NET USE
from powershell sometimes works, sometimes don't, but still it appears that there's a difference between CMD and PS.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).At first glance one really wants to use New-PSDrive supplying it credentials.
Fails!
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:
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!