When working with WebDAV servers in Windows environments, you might need to map a network drive programmatically. While manual mapping through Windows Explorer works fine (using "Map network drive" with credentials), PowerShell presents some unique challenges.
The New-PSDrive
cmdlet has limitations when dealing with WebDAV:
# This will fail with WebDAV URLs
New-PSDrive -Name Z -PSProvider FileSystem -Root "http://webdav.example.com/" -Persist
The error occurs because:
- WebDAV URLs aren't recognized as valid filesystem paths
- The -Persist parameter requires traditional UNC paths
The most reliable method is using the legacy net use
command through PowerShell:
$webdavUrl = "http://webdavservertest3.cloudapp.net/"
$driveLetter = "Z:"
$credentials = Get-Credential
# Map the drive
$username = $credentials.UserName
$password = $credentials.GetNetworkCredential().Password
$netUseCommand = "net use $driveLetter "$webdavUrl" /user:"$username" "$password" /persistent:yes"
Invoke-Expression $netUseCommand
For automation scenarios, consider these approaches:
# Option 1: Encrypted credential file (first run)
$credPath = "C:\secure\webdav.cred"
Get-Credential | Export-Clixml -Path $credPath
# Subsequent runs
$storedCred = Import-Clixml -Path $credPath
$username = $storedCred.UserName
$password = $storedCred.GetNetworkCredential().Password
After mapping, verify with:
# Check mapped drives
Get-PSDrive -PSProvider FileSystem | Where-Object { $_.DisplayRoot -like "*webdav*" }
# Test file operations
Test-Path "Z:\"
New-Item -ItemType File -Path "Z:\testfile.txt" -Force
If you encounter problems:
- Ensure WebClient service is running:
Get-Service WebClient
- Check registry settings for WebDAV:
HKLM\SYSTEM\CurrentControlSet\Services\WebClient\Parameters
- Verify BasicAuthentication is enabled in IIS if using HTTP
For better compatibility, consider configuring your WebDAV server with a virtual directory:
# This UNC-style path often works better with PowerShell
$uncPath = "\\webdavservertest3.cloudapp.net@SSL\DavWWWRoot\"
New-PSDrive -Name Z -PSProvider FileSystem -Root $uncPath -Persist
After successfully configuring an Azure VM with IIS WebDAV, manual mapping through Windows Explorer works perfectly with credentials. However, when attempting automation via PowerShell's New-PSDrive
, we encounter two distinct errors:
- With
-Persist
: "DriveRootNotNetworkPath" error stating root must be a file system location - Without
-Persist
: "DriveRootError" claiming the path doesn't exist
The core issue stems from New-PSDrive
expecting UNC paths (like \\server\share
) rather than HTTP URLs. WebDAV requires special handling through the WebClient service.
For persistent WebDAV mapping, we need to use the traditional net use
command instead:
$serviceName = "webdavservertest3.cloudapp.net"
$networkDrive = "Z:"
$credential = Get-Credential
# Map with credentials
net use $networkDrive "http://$serviceName/" /persistent:yes /user:$($credential.UserName) $($credential.GetNetworkCredential().Password)
Here's a production-ready script with error handling:
function Map-WebDAVDrive {
param(
[string]$ServiceName,
[string]$DriveLetter,
[PSCredential]$Credential,
[bool]$Persistent = $true
)
# Verify WebClient service is running
if ((Get-Service WebClient).Status -ne "Running") {
Start-Service WebClient -ErrorAction Stop
}
# Build connection string
$persistFlag = if ($Persistent) { "/persistent:yes" } else { "/persistent:no" }
$password = $Credential.GetNetworkCredential().Password
try {
$result = net use "${DriveLetter}:" "http://${ServiceName}/" $persistFlag /user:$($Credential.UserName) $password 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Mapping failed: $result"
}
Write-Output "Successfully mapped ${DriveLetter}: to WebDAV share"
}
catch {
Write-Error "WebDAV mapping error: $_"
}
}
# Usage example
$creds = Get-Credential -Message "Enter WebDAV credentials"
Map-WebDAVDrive -ServiceName "webdavservertest3.cloudapp.net" -DriveLetter "Z" -Credential $creds
For advanced scenarios, you can modify the registry to treat WebDAV as a network location:
# Requires admin privileges
$regPath = "HKLM:\SYSTEM\CurrentControlSet\Services\WebClient\Parameters"
$valueName = "BasicAuthLevel"
$desiredValue = 2 # Basic authentication over SSL
if ((Get-ItemProperty -Path $regPath -Name $valueName).$valueName -lt $desiredValue) {
Set-ItemProperty -Path $regPath -Name $valueName -Value $desiredValue
Restart-Service WebClient
}
If mapping still fails:
- Verify WebClient service is running (
Get-Service WebClient
) - Check if HTTP.SYS has URL reservation (
netsh http show urlacl
) - Test basic connectivity (
Test-NetConnection $serviceName -Port 80
) - Validate credentials with
Invoke-WebRequest