1

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.

Gordon
  • 6,257
  • 6
  • 36
  • 89
  • As an aside, I think this should cover you for a [UNC path regex](https://regex101.com/r/1B8Cxd/2). – Ash Sep 01 '20 at 08:46
  • @ash, I wonder if you could help me understand why the first \ is outside the non capturing group, while the second one is part of the non capturing group? – Gordon Sep 01 '20 at 15:57
  • Aha, I think I see it. It's there to catch not only the first instance of \someothercharacters but also any further folders in the path. – Gordon Sep 01 '20 at 16:05
  • Consider adding your findings as an answer to you question instead of an Edit, so that you can mark it solved. – Dennis Aug 26 '21 at 21:37

0 Answers0