Mapping a network drive mounts a remote Shared Resource (SMB share) to a local drive letter, allowing file access over the network as if it were a local volume. PowerShell replaces the legacy net use command with the SmbShare module, providing dedicated cmdlets (New-SmbMapping, Get-SmbMapping, and Remove-SmbMapping) to manage persistent or session-based SMB connections with structured object output.
Legacy net use vs. Modern SMB Cmdlets
The traditional net use utility emits unformatted text strings and requires fragile error handling. PowerShell's SmbShare cmdlets output structured MSFT_SmbMapping CIM objects, allowing scripts to inspect share availability, authentication status, and reconnect behaviors programmatically.
Legacy net.exe Command |
Modern PowerShell Cmdlet | Scope / Behavior |
|---|---|---|
net use |
Get-SmbMapping |
Lists mapped SMB shares and their operational status. |
net use Z: \\server\share |
New-SmbMapping -LocalPath "Z:" -RemotePath "\\server\share" |
Maps a remote UNC share to a local drive letter. |
net use Z: \\server\share /user:domain\user password |
New-SmbMapping -LocalPath "Z:" -RemotePath "\\server\share" -Credential $cred |
Maps a share with explicit credentials. |
net use Z: /delete |
Remove-SmbMapping -LocalPath "Z:" -Force |
Disconnects and deletes an active network mapping. |
net use * /delete /y |
Get-SmbMapping | Remove-SmbMapping -Force |
Clears all mapped network drives non-interactively. |
Creating Network Mappings with New-SmbMapping
The New-SmbMapping cmdlet establishes a connection between a local drive letter and a remote Universal Naming Convention (UNC) path (\\Server\Share).
New-SmbMapping -LocalPath "Z:" -RemotePath "\\fileserver01\shared" -Persistent $truewhere:
-LocalPathis the local drive letter to assign (e.g.,Z:).-RemotePathis the UNC path of the remote share.-Persistentwhen$true, the mapping is automatically restored after a reboot.
Status Local Path Remote Path
------ ---------- -----------
OK Z: \\fileserver01\sharedComplete Mapping Example with Persistence
New-SmbMapping -LocalPath "Z:" `
-RemotePath "\\fs01.corp.internal\SharedDocs" `
-Persistent $true `
-SaveCredentialwhere:
New-SmbMappingis the cmdlet that establishes the SMB connection and drive binding.-LocalPathis the target local drive letter followed by a colon (e.g.,Z:).-RemotePathis the absolute UNC path specifying the remote file server and target SMB share.-Persistent $trueconfigures Windows to automatically reconnect the mapped drive across subsequent user logon sessions.-SaveCredentialinstructs the operating system to store supplied authentication credentials in Windows Credential Manager.
./map-network-drive.ps1Status Local Path Remote Path
------ ---------- -----------
OK Z: \\fs01.corp.internal\SharedDocsMapping with Credentials
For shares that require authentication with a different account than the current session.
$cred = Get-Credential
New-SmbMapping -LocalPath "Y:" -RemotePath "\\fileserver01\finance" -Credential $cred -Persistent $truewhere -Credential is a PSCredential object supplying the username/password for the connection.
Mapping Without a Drive Letter
A share can also be mapped without assigning a persistent drive letter, useful for temporary or scripted access.
New-SmbMapping -RemotePath "\\fileserver01\shared"Viewing Mapped Drives with Get-SmbMapping
The Get-SmbMapping cmdlet inspects active SMB connections, listing all current network drive mappings on the local machine along with their status.
Get-SmbMappingStatus Local Path Remote Path Persistent
------ ---------- ----------- ----------
OK Z: \\fileserver01\shared True
OK Y: \\fileserver01\finance TrueFiltering by Status
Get-SmbMapping | Where-Object { $_.Status -ne "OK" }Selective Property Inspection
Get-SmbMapping | Select-Object -Property LocalPath, RemotePath, Statuswhere:
Get-SmbMappingqueries and lists all mounted SMB connections for the current user profile.Select-Objectfilters for the drive letter, target server path, and operational health.
./manage-smb-mappings.ps1LocalPath RemotePath Status
--------- ---------- ------
Z: \\fs01.corp.internal\SharedDocs OKRemoving a Mapping with Remove-SmbMapping
The Remove-SmbMapping cmdlet disconnects a network drive mapping and releases the local drive letter.
Remove-SmbMapping -LocalPath "Z:" -Forcewhere:
-LocalPathis the drive letter (or UNC path) to disconnect.-Forcesuppresses the confirmation prompt and tears down active file handles if files on the share are currently open.-UpdateProfileremoves the persistent entry from the user's registry profile so Windows does not attempt to reconnect the drive on next boot.
Removing All Mappings
Get-SmbMapping | Remove-SmbMapping -ForceSafe Disconnection with Profile Cleanup
Remove-SmbMapping -LocalPath "Z:" -Force -UpdateProfilewhere -UpdateProfile removes the persistent entry from the user's registry profile so Windows does not attempt to reconnect the drive on next boot.
Practical Usage: Ensuring a Drive Mapping Exists at Script Start
A common pattern in automation scripts is checking whether a required network drive is already mapped, and creating it only if missing.
param(
[string]$DriveLetter = "Z:",
[string]$SharePath = "\\fileserver01\shared"
)
$existing = Get-SmbMapping -LocalPath $DriveLetter -ErrorAction SilentlyContinue
if ($existing -and $existing.Status -eq "OK") {
Write-Output "$DriveLetter is already mapped to $($existing.RemotePath)."
} else {
if ($existing) {
Remove-SmbMapping -LocalPath $DriveLetter -Force
}
New-SmbMapping -LocalPath $DriveLetter -RemotePath $SharePath -Persistent $true
Write-Output "$DriveLetter mapped to $SharePath."
}where:
$existingchecks for a pre-existing mapping on the target drive letter.- The script removes any stale mapping (wrong remote path or broken status) before re-creating it.
./ensure-drive-mapped.ps1 -DriveLetter "Z:" -SharePath "\\fileserver01\shared"Z: mapped to \\fileserver01\shared.Practical Administration: Safe Reconnection and Fallback Pipeline
In corporate environments, automated logon scripts must gracefully verify remote share reachability, unmount stale or disconnected mappings, and mount the required share without throwing terminating errors.
param (
[string]$DriveLetter = "M:",
[string]$ServerName = "fs01.corp.internal",
[string]$ShareName = "Marketing"
)
$targetUnc = "\\$ServerName\$ShareName"
Write-Output "Verifying SMB connectivity to $ServerName (Port 445)..."
$canConnect = (Test-NetConnection -ComputerName $ServerName -Port 445 -WarningAction SilentlyContinue).TcpTestSucceeded
if (-not $canConnect) {
Write-Error "Cannot reach SMB port on $ServerName. Mapping aborted."
return
}
$existing = Get-SmbMapping -LocalPath $DriveLetter -ErrorAction SilentlyContinue
if ($existing) {
if ($existing.RemotePath -eq $targetUnc -and $existing.Status -eq "OK") {
Write-Output "Drive $DriveLetter is already mapped to $targetUnc."
return
}
else {
Write-Warning "Drive $DriveLetter is bound to $($existing.RemotePath). Replacing mapping..."
Remove-SmbMapping -LocalPath $DriveLetter -Force -UpdateProfile
}
}
New-SmbMapping -LocalPath $DriveLetter -RemotePath $targetUnc -Persistent $true
Write-Output "Successfully mapped $DriveLetter to $targetUnc"where:
Test-NetConnection -Port 445validates that the remote host is reachable on the standard SMB port before attempting the mount operation.-ErrorAction SilentlyContinueprevents script termination ifGet-SmbMappingfinds no existing mapping for the specified drive letter.Remove-SmbMapping -UpdateProfileunbinds stale drives cleanly before reassigning the drive letter.
./mount-corporate-share.ps1 -DriveLetter "M:" -ServerName "fs01.corp.internal" -ShareName "Marketing"Verifying SMB connectivity to fs01.corp.internal (Port 445)...
Successfully mapped M: to \\fs01.corp.internal\Marketing