Reputation: 11679
The aim is to validate whether a parameter could be executed before passing it to a certain Powershell function to avoid the following error:
& : The term 'c:\helloworld' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\functions\function.ps1:67 char:9
+ & $createServiceCommand install $serviceName
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (c:\helloworld:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Upvotes: 1
Views: 58
Reputation: 24525
if ( get-command helloworld -erroraction silentlycontinue ) {
"Command was found"
}
else {
"Command was not found"
}
Upvotes: 3
Reputation: 304
You can use Test-Path cmdlet to check for the existence of the path.
Upvotes: -1