Gordon
Gordon

Reputation: 6883

Validate "path"

The following code works to run powercfg.exe, but I would like to make it more flexible and user friendly by testing for powercfg.exe and exiting gracefully with a logged error when the user provides a bad $executable. It seems that the file is being found in some part of %path%, but Test-Path is not resolving the path the same way Start-Process does. Is there some mechanism to expand the path automatically, or do i need to actually use the Path env var to do this manually?

$resource = 'C:\rtc.pow'
$executable = "powercfg.exe"
$argumentList = "-import $resource d03b6c96-607f-412c-b47b-417fa8d391af"
Start-Process -FilePath:$executable -argumentList:$argumentList

Upvotes: 0

Views: 81

Answers (1)

Vesper
Vesper

Reputation: 18757

You can use get-command powercfg.exe and get its Path attribute to receive full name of the executable.

PS C:\Users\me> get-command powercfg.exe | select -expand path
C:\Windows\system32\powercfg.exe

Upvotes: 1

Related Questions