5 Sep
I have several PowerShell scripts and functions that I’ve written in the last few months which take a file or folder path as an argument, and the problem is that most of them need to validate the path in one way or another, and generally speaking this means that you have to pass in a fully qualified path, because they have no simple way to find the file if you pass in a partial, relative path, or the name of a file that’s on your system path.
Anyway, I finally worked up a function to resolve paths taking the current directory and the environment path variable into account:
It’s actually pretty simple … it uses the Test-Path method to see if you passed in a full path, and otherwise searches the current directory and all the directories in the PATH to find a matching item. You can specify if you want the -Type "Leaf" (e.g.: files) or -Type "Container" (e.g.: folders) and you can set the -All switch to return all matches in the path instead of just the first one. As you may have guessed, the path doesn’t necessarily have to be a FileSystem path, it will resolve other PSProvider paths based on the current Location. I have it search the paths in the Environment:\Path because I’m mostly concerned with file and folder paths … but aside from that, there’s no equivalent collection of paths for other providers … maybe someone should add some …