Testing UNC Paths
Test-Path can test whether or not a given file or folder exists. This works fine for paths that use a drive letter, but can fail...
- Written By: ps1
- Last Updated: April 22, 2025
- 2 minutes read
Test-Path can test whether or not a given file or folder exists. This works fine for paths that use a drive letter, but can fail...
Test-Path can test whether or not a given file or folder exists. This works fine for paths that use a drive letter, but can fail with pure UNC paths.
At its simplest, this should return $true, and it does (provided you did not disable your administrative shares):
$path = '\\127.0.0.1\c$' Test-Path -Path $path
Now, the very same code can also return $false:
Set-Location -Path HKCU:\ $path = '\\127.0.0.1\c$' Test-Path -Path $path
If a path does not use a drive letter, PowerShell falls back to the current path, and if that path happens to point to a non-file system location, Test-Path interprets the UNC path in the context of that provider. Since there is no such path in your Registry, Test-Path returns $false.
To make Test-Path work reliably with UNC paths, make sure you prepend the UNC path with the FileSystem provider. Now, the result is valid regardless of current drive location:
Set-Location -Path HKCU:\ $path = 'filesystem::\\127.0.0.1\c$' Test-Path -Path $path
Contact our sales team to get a personalized demo of our database management software for SQL Server!