Connect to Network Drive with User Name and Password

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



Leave a reply



Submit