Unable to run executables in PowerShell Core on macOS

Unable to run executables in PowerShell Core on macOS

So you installed PowerShell Core on macOS, but can't run any of your executables. Here is how to fix it.

PowerShell on any platform

Originally developed for Windows, PowerShell is a powerful shell for automating all kinds of system management work. With PowerShell Core, Microsoft brought it to platforms other than Windows, and it's totally worth the switch from *sh, which in comparison feel very archaic.

PowerShell has built-in support for working with JSON objects and working with collections and arrays just works without getting a PhD. If you're working with JavaScript, TypeScript or Microsoft technologies, you will find PowerShell more intuitive than Bash.

Inconvenient PowerShell Core on macOS

PowerShell Core is just a shell. While natively it works with cmdlets, in the end, it's just a shell and you can use it to run any executable on your machine, including Node.js executables like the Office 365 CLI.

Before you can call any executable on your machine, you have to add its location to the environment path, which in PowerShell is stored in the $env:PATH variable. There are plenty of articles explaining how you can do that. Unfortunately, it could be that even after following them to the letter, you will see an error like:

PS /Users/waldek> dotnet
dotnet : The term 'dotnet' 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 line:1 char:1
+ dotnet
+ ~~~~~~
+ CategoryInfo          : ObjectNotFound: (dotnet:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

The same goes for node or any other executable:

PS /Users/waldek> node
node : The term 'node' 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 line:1 char:1
+ node
+ ~~~~~~
+ CategoryInfo          : ObjectNotFound: (dotnet:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

If you haven't disabled automatically loading Modules in your PowerShell profile, it could be that calling node, PowerShell will load its DSC Module rather than showing the aforementioned error.

So how to solve it?

Adding environment paths in PowerShell Core on macOS

If you follow the different articles on the Internet, you would likely add environment paths, by editing your profile file and appending paths to $env:PATH, eg.:

$env:PATH += ";/Users/waldek/.nvm/versions/node/v10.15.3/bin;/usr/local/share/dotnet"

Unfortunately, this setup will lead to the errors I mentioned earlier. The approach in itself is correct, but what you need to pay attention to, is that you separate the paths with a colon : and not a semi-colon ;:

$env:PATH += ":/Users/waldek/.nvm/versions/node/v10.15.3/bin:/usr/local/share/dotnet"

This will fix the issue and let you use your executables as expected.

Comments

comments powered by Disqus