ridicurious.com
Powershell, Productivity, PSTip

PowerShell Environment Path and Command discovery

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

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

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.

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

or just tweak the registries to  make it permanent

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

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

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

 

Related posts

PowerShell : Retrieving Exchange attributes from Active Directory

Prateek Singh
9 years ago

Get Online Dictionary Words using PowerShell

Prateek Singh
7 years ago

Escape Special Characters using [RegEx] in PowerShell

Prateek Singh
7 years ago
Exit mobile version