QuickTip: How to validate a UNC path
The System.Uri class provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI. One of its properties is IsUnc . IsUnc gets whether the specified Uri is a universal naming convention (UNC) path and returns a Boolean value that is True if the Uri is a UNC path or False otherwise.
#Requires -Version 2.0
function Test-UNC
{
param(
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[Alias('FullName')]
[System.String[]]$Path
)
process
{
foreach($p in $Path)
{
[bool]([System.Uri]$p).IsUnc
}
}
}
Usage
# 1. Test the paths of a remote share
PS > Get-ChildItem \\server\share | Test-UNC
True
True
(...)
# 2. Pipe values
PS > "\\server\share","C:\autoexec.bat" | Test-UNC
True
False
# 3. Test command line values
PS > Test-UNC -Path "\\server\share","C:\autoexec.bat","file://server/filename.ext"
True
False
True
# 4. Test local paths
PS > Get-ChildItem C:\ | Test-UNC
False
False
(...)