Complete Guide to Connecting Network Folders with Username/Password in PowerShell

Nov 22, 2025 · Programming · 14 views · 7.8

Keywords: PowerShell | Network Shares | Authentication | New-PSDrive | UNC Paths

Abstract: This technical paper provides an in-depth analysis of various methods for connecting to network shared folders requiring authentication in PowerShell. It examines the behavioral differences of the New-PSDrive command across different PowerShell versions, presents alternative approaches using WScript.Network COM objects and net use commands, and demonstrates implementation details through practical code examples. The paper also discusses limitations and best practices for using UNC paths in PowerShell, offering comprehensive technical guidance for system administrators and developers.

Overview of Network Share Connectivity in PowerShell

In Windows environments, accessing network shared folders constitutes an essential component of daily system administration tasks. Unlike Windows Explorer, PowerShell does not automatically prompt users for credentials when accessing network shares that require authentication. This discrepancy often poses challenges for system administrators, particularly when automated scripts need to access protected network resources.

Historical Evolution of New-PSDrive Command

Superficially, the New-PSDrive command appears to be the ideal choice for connecting to network shares. While the command's documentation indicates acceptance of PSCredential objects as parameters, the FileSystem provider in early PowerShell versions did not actually support credential parameters. Attempting to use the following command generates an error:

New-PSDrive -Name P -PSProvider FileSystem -Root \\server\share -Credential domain\user

The system returns the error message: "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."

Improvements in PowerShell 3.0 and Later Versions

With the release of PowerShell 3.0, the New-PSDrive command received significant enhancements and can now successfully map network shares requiring credentials:

New-PSDrive -Name P -PSProvider FileSystem -Root \\Server01\Public -Credential user\domain -Persist

The -Persist parameter ensures that the drive mapping remains active after the PowerShell session ends, which is particularly useful for scenarios requiring persistent connections.

Using WScript.Network COM Object

For earlier PowerShell versions or situations requiring finer control, the MapNetworkDrive method of the WScript.Network COM object can be employed:

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("u:", "\\server\share", $false, "domain\user", "password")

This approach directly invokes Windows' underlying network mapping functionality, providing a reliable authentication mechanism. The first parameter specifies the drive letter, the second parameter is the share path, the third parameter controls reconnection at logon, and the final two parameters provide username and password respectively.

Integration with Traditional net use Command

As an alternative approach, the traditional net use command can be invoked within PowerShell:

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

While this method is not entirely native to PowerShell, it excels in compatibility and reliability, especially when dealing with complex network environments.

Usage of UNC Paths in PowerShell

PowerShell fully supports UNC paths, which can be used directly in most file system operations:

Set-Location \\servername\sharename
Get-ChildItem \\servername\sharename
Copy-Item \\servername1\sharename1\filename.ext \\servername2\sharename2
Remove-Item \\servername\sharename\foldername\filename.ext

However, to successfully use these commands, network connectivity to the remote share must first be established. In some cases, it may be necessary to browse the share path through Windows Explorer to trigger the credentials dialog, or employ the authentication methods mentioned earlier.

Access Rights and Credential Management

The key to successfully connecting to network shares lies in proper access rights configuration. If the network share resides in a different domain, the username must follow the domain\username format. For scenarios requiring administrator privileges, connections must be established through elevated PowerShell instances and File Explorer.

Practical Function Example

To simplify network location navigation, custom functions can be created:

function ncd {
    param(
        [Parameter(ValueFromPipeline=$true)]
        [string[]]$netLocation
    )
    $netLocation = 'Microsoft.PowerShell.Core\FileSystem::'+$netlocation
    cd $ExecutionContext.InvokeCommand.ExpandString($netlocation)
}

Usage example: ncd \\servername\sharename$, which switches to the specified network location.

Best Practices and Considerations

When selecting connection methods, consider PowerShell version, security requirements, and persistence needs. For automated scripts, using New-PSDrive with the -Persist parameter is recommended; for temporary connections, the WScript.Network method may be more appropriate. Always pay attention to secure credential storage and transmission, avoiding hardcoded passwords in scripts.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.