030
030

Reputation: 11679

How to validate whether a path could be executed using Powershell?

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

Answers (2)

Bill_Stewart
Bill_Stewart

Reputation: 24525

if ( get-command helloworld -erroraction silentlycontinue ) {
  "Command was found"
}
else {
  "Command was not found"
}

Upvotes: 3

Antony Francis
Antony Francis

Reputation: 304

You can use Test-Path cmdlet to check for the existence of the path.

Upvotes: -1

Related Questions