How to provide user name and password when connecting to a network share
You can either change the thread identity, or P/Invoke WNetAddConnection2. I prefer the latter, as I sometimes need to maintain multiple credentials for different locations. I wrap it into an IDisposable and call WNetCancelConnection2 to remove the creds afterwards (avoiding the multiple usernames error):
using (new NetworkConnection(@"\\server\read", readCredentials))
using (new NetworkConnection(@"\\server2\write", writeCredentials)) {
File.Copy(@"\\server\read\file", @"\\server2\write\file");
}
Connecting to a network folder with username/password in Powershell
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
Batch file to map an external network drive and prompt for username and password
I was able to figure this out and I want to post the answer because I am sure there are many others with the same question. Since I am mapping an external network drive, I added a cmdkey.exe command to add the user credentials to the Credential Manager, once it was added I then net use the network drive and folder to the Z drive. Worked like a charm. Hope this helps.
@echo off
if exist net use z: /delete
echo Please enter User ID:
set /p USERID=
echo Please enter your Password:
set /p PASSWORD=
cmdkey.exe /add:*.sample.com /user:DOMAIN\%USERID% /pass:%PASSWORD%
net use Z: "\\SERVER.DOMAIN.COM\workgroup"
pause
How to access network drive folder using credentials in php?
This works for me:
<?php
$user = 'mickeymouse';
$password = 'helloworld';
exec('net use "\\\INVEST-OP-001\test\music" /user:"'.$user.'" "'.$password.'" /persistent:no');
$files = scandir('\\\INVEST-OP-001\test\music');
echo '<pre>';
print_r($files);
exec('net use "\\\INVEST-OP-001\test\music" /delete /yes');
Determine Domain and username used to map a network drive
WMI is your friend:
> wmic netuse where LocalName="Z:" get UserName /value
UserName=rd-pc2037\Administrator
[anonymous suggestion 2022-08-07]:
Since Microsoft is gradually moving away from WMI,
Powershell/CIM is your future friend:
Get-CimInstance -classname Win32_NetworkConnection | select-object Remotename,Username
Powershell: how to map a network drive with a different username/password
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("r:", "\\romeobox\files", $false, "domain\user", "password")
Should do the trick,
Kindness,
Dan
Related Topics
Why Does the Contains() Operator Degrade Entity Framework's Performance So Dramatically
Why Are Private Fields Private to the Type, Not the Instance
Mirroring Console Output to a File
Fast and Compact Object Serialization in .Net
Filter/Search Using Multiple Fields - ASP.NET MVC
Why Are C# 4 Optional Parameters Defined on Interface Not Enforced on Implementing Class
Using the "Params" Keyword for Generic Parameters in C#
Compare Equality Between Two Objects in Nunit
Simplest Way to Do a Fire and Forget Method in C# 4.0
Mysqlcommand Command.Parameters.Add Is Obsolete
How to Select Text from the Richtextbox and Then Color It
Why Does Resharper Want to Use 'Var' for Everything
Posting JSON Data to ASP.NET MVC
Does Entity Framework Code First Support Stored Procedures
How to Draw Directly on the Windows Desktop, C#
Don't Stop Debugger at That Exception When It's Thrown and Caught