I need to be able to test the validity of a path, regardless of actual presence. My function is used to validate a user provided path, which might be a local drive and might be a UNC path and might be a registry path. And the path could then be used as the location to create a new folder, delete the path, as the source or destination of a copy, etc.
Test-Path $path -isValid
works, with one exception, when the path is the root of a UNC share.
So when $path = '\\Server\Folder'
I get true, but when $path = '\\Server'
I get false, even though \\Server
is a perfectly valid path.
I found this which suggests using ([System.Uri]$path).IsUnc
but that returns true when $path = '\\Px\Folder\|'
which is in fact a totally invalid path.
I can probably expand my logic to test if a path is in fact a share root, then handle things differently, but I wonder if I am missing some built in solution that actually works across all situations as I expected Test-Path -isValid
to do. The fact that this is still broken in the PS 5.1 I am testing on is a bit disappointing. Or am I missing something and no rational person who'll want to test \\Server
as a valid path because no one would copy a file to such a path, or create a folder there? That is of course a rhetorical equation and sarcasm, I had had MANY times when I needed to do exactly that, thus the attempt to automate such things.
On a slight tangent, is there an accepted RegEx for validating a UNC path, that handles invalid characters appropriately? If I had a set of RegEx expressions so I could ID a local path, UNC path or a Registry path, then validate each for disallowed characters, that could work.
EDIT: I am an idiot. $path = '\\Server'
is NOT a valid path, you need both a server AND a share to define a valid path. That'll teach me to do RegEx stuff while tired.