Manage Persistent Network Drives with PowerShell
Inspect, map, remap, and remove persistent Windows network drives with PowerShell.
New-PSDrive can create Windows mapped drives that remain available in File Explorer. Use a small script to check the current state before replacing a mapping.
Inspect Existing Drives
1
2
Get-PSDrive -PSProvider FileSystem
Get-PSDrive -Name V -ErrorAction SilentlyContinue
Map a Persistent Drive
1
2
3
4
5
6
New-PSDrive `
-Name V `
-PSProvider FileSystem `
-Root \\VM-server\Folder-01 `
-Persist `
-Scope Global
-Persist creates a Windows mapped network drive. The drive letter must be available and the root must be a UNC path such as \\Server\Share.
Remap a Drive Safely
1
2
3
4
5
6
7
8
9
10
11
12
$existing = Get-PSDrive -Name V -ErrorAction SilentlyContinue
if ($existing) {
Remove-PSDrive -Name V -Force
}
New-PSDrive `
-Name V `
-PSProvider FileSystem `
-Root \\VM-server\Folder-02 `
-Persist `
-Scope Global
Review the current root before removing a mapping that another script may depend on.
Use a Stored Credential
1
2
3
4
5
6
7
8
9
10
11
$credential = Get-Secret `
-Name NetworkShareCredential `
-Vault SecretStore
New-PSDrive `
-Name V `
-PSProvider FileSystem `
-Root \\VM-server\Folder-01 `
-Persist `
-Scope Global `
-Credential $credential
See Store PowerShell Credentials Safely with SecretStore for the vault setup.
Run Scripts in the Right Scope
Microsoft notes that a persistent mapping created inside a script may not remain after the script scope ends. Use -Scope Global, and dot-source a script when you intentionally want its mapping to remain in the calling session:
1
. .\Map-Drive.ps1
Mapped drives are user-specific. A drive created in an elevated session may not appear in a non-elevated session.