I often see people wondering that some commands/applications launch from Windows Run (Windows Key+R) but when I try them in PowerShell console it throws an error, I know this gets frustrating at times. For example, when I try to run commands like Excel or WinWord my PowerShell console throws errors, but When I launch Notepad it just works fine from both PowerShell and RUN.

1

To understand the behavior, let’s take a look at how command discovery works in PowerShell. During the process of command discovery PowerShell looks into all the folders defined in Environment variable PATH  ($Env:Path) and nothing beyond that, like in the below screenshot PowerShell was unable to discover the command in any of the folders mentioned in $Env:Path and fails to execute it.

Trace-Command -Expression {excel} -Name CommandDiscovery -PSHost

1

Just add the target folder where you have your executable in the $Env:Path variable and you are good to go.

1

Now PowerShell would be able to discover your command and launch it like in the below image, but these changes to $Env:Path variable is temporary and disappear when the session is closed.

2

gif

To work around that either you add the Folder path to –
System Properties > Advanced > Environment Variables > PATH

3

or just tweak the registries to  make it permanent

1

Apart from this, you can use the .NET API’s to achieve this as well, like in the following image

1

Hoping you’ll find this article useful, and thanks for reading. Cheers!

signature [twitter-follow screen_name=’singhprateik’ show_count=’yes’]