Skip to content

PowerShell Network Drive Mapping

How to use PowerShell's New-SmbMapping, Get-SmbMapping, and Remove-SmbMapping cmdlets to map, manage, and remove network drives in Windows, including persistent connections and credential handling.

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).

Consolepowershell
New-SmbMapping -LocalPath "Z:" -RemotePath "\\fileserver01\shared" -Persistent $true

where:

  • -LocalPath is the local drive letter to assign (e.g., Z:).
  • -RemotePath is the UNC path of the remote share.
  • -Persistent when $true, the mapping is automatically restored after a reboot.
Outputplaintext
Status Local Path Remote Path
------ ---------- -----------
OK     Z:         \\fileserver01\shared

Complete Mapping Example with Persistence

map-network-drive.ps1powershell
New-SmbMapping -LocalPath "Z:" `
    -RemotePath "\\fs01.corp.internal\SharedDocs" `
    -Persistent $true `
    -SaveCredential

where:

  • New-SmbMapping is the cmdlet that establishes the SMB connection and drive binding.
  • -LocalPath is the target local drive letter followed by a colon (e.g., Z:).
  • -RemotePath is the absolute UNC path specifying the remote file server and target SMB share.
  • -Persistent $true configures Windows to automatically reconnect the mapped drive across subsequent user logon sessions.
  • -SaveCredential instructs the operating system to store supplied authentication credentials in Windows Credential Manager.
Consolepowershell
./map-network-drive.ps1
Outputplaintext
Status Local Path Remote Path
------ ---------- -----------
OK     Z:         \\fs01.corp.internal\SharedDocs

Mapping with Credentials

For shares that require authentication with a different account than the current session.

Consolepowershell
$cred = Get-Credential
New-SmbMapping -LocalPath "Y:" -RemotePath "\\fileserver01\finance" -Credential $cred -Persistent $true

where -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.

Consolepowershell
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.

Consolepowershell
Get-SmbMapping
Outputplaintext
Status Local Path Remote Path              Persistent
------ ---------- -----------              ----------
OK     Z:         \\fileserver01\shared    True
OK     Y:         \\fileserver01\finance   True

Filtering by Status

Consolepowershell
Get-SmbMapping | Where-Object { $_.Status -ne "OK" }

Selective Property Inspection

manage-smb-mappings.ps1powershell
Get-SmbMapping | Select-Object -Property LocalPath, RemotePath, Status

where:

  • Get-SmbMapping queries and lists all mounted SMB connections for the current user profile.
  • Select-Object filters for the drive letter, target server path, and operational health.
Consolepowershell
./manage-smb-mappings.ps1
Outputplaintext
LocalPath RemotePath                        Status
--------- ----------                        ------
Z:        \\fs01.corp.internal\SharedDocs   OK

Removing a Mapping with Remove-SmbMapping

The Remove-SmbMapping cmdlet disconnects a network drive mapping and releases the local drive letter.

Consolepowershell
Remove-SmbMapping -LocalPath "Z:" -Force

where:

  • -LocalPath is the drive letter (or UNC path) to disconnect.
  • -Force suppresses the confirmation prompt and tears down active file handles if files on the share are currently open.
  • -UpdateProfile removes the persistent entry from the user's registry profile so Windows does not attempt to reconnect the drive on next boot.

Removing All Mappings

Consolepowershell
Get-SmbMapping | Remove-SmbMapping -Force

Safe Disconnection with Profile Cleanup

Consolepowershell
Remove-SmbMapping -LocalPath "Z:" -Force -UpdateProfile

where -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.

ensure-drive-mapped.ps1powershell
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:

  • $existing checks 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.
Consolepowershell
./ensure-drive-mapped.ps1 -DriveLetter "Z:" -SharePath "\\fileserver01\shared"
Outputplaintext
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.

mount-corporate-share.ps1powershell
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 445 validates that the remote host is reachable on the standard SMB port before attempting the mount operation.
  • -ErrorAction SilentlyContinue prevents script termination if Get-SmbMapping finds no existing mapping for the specified drive letter.
  • Remove-SmbMapping -UpdateProfile unbinds stale drives cleanly before reassigning the drive letter.
Consolepowershell
./mount-corporate-share.ps1 -DriveLetter "M:" -ServerName "fs01.corp.internal" -ShareName "Marketing"
Outputplaintext
Verifying SMB connectivity to fs01.corp.internal (Port 445)...
Successfully mapped M: to \\fs01.corp.internal\Marketing
Navigation