Safely Use UNC Paths

by Apr 20, 2015

Whenever you use UNC paths in PowerShell, your script may break. Since a UNC path has no drive letter, PowerShell looks at the current directory instead, and uses the PSProvider attached to the current directory.

So if your current directory is not a file path (but instead the registry drive, for example), your UNC path is interpreted as registry path.

To play safe, always add the correct provider name to UNC paths:

# UNC-Paths have no drive letter
# so PowerShell uses the current directory instead to find the PSProvider
# for UNC paths, to be safe, always add the provider name manually
$exists = Test-Path -Path 'FileSystem::\\server12\fileshare'

$exists

Twitter This Tip! ReTweet this Tip!