DCSIMG
QuickTip: How to validate a UNC path - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

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
(...)

Comments

Twitter Trackbacks for QuickTip: How to validate a UNC path - Shay Levy [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 QuickTip: How to validate a UNC path - Shay Levy         [microsoft.co.il]        on Topsy.com

# May 27, 2010 3:27 PM

Doug said:

Nice and useful.

What about Test-IsUNC ?

# May 27, 2010 4:08 PM

ScriptFanatic said:

Good question Doug :)

# May 29, 2010 12:27 PM

Jeremy Saunders said:

Nice Blog Shay...Very Useful! :-)

# November 3, 2011 8:30 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: