Scott Hanselman

Installing PowerShell with one line as a .NET Core global tool

July 18, '19 Comments [4] Posted in DotNetCore | Open Source | PowerShell
Sponsored By

I'PowerShell Mascotm a huge fan of .NET Core global tools. I've done a podcast on Global Tools. Just like Node and other platform have globally tools that can be easily and quickly installed and then used in build scripts, CI/CD (Continuous Integration/Continuous Deployment) systems, or just general command line utilities, .NET Global Tools are easily made (by you!) and distributed via NuGet.

Some cool examples (and there are hundreds) are the "Try .NET" Workshop runner and creator that can you can use to make interactive documentation, or coverlet for code coverage. There's a great and growing list of .NET Core Global Tools on GitHub.

If you've got the .NET SDK installed you can try out a global tool just like this.

dotnet tool install -g dotnetsay

Then run this example with "dotnetsay," it's fun.

stepping back a moment, you may be familiar with PowerShell. It's a scripting language and a command line shell like Bash or DOS or the Windows Command Prompt. You may think of PowerShell as a tool for maintaining and managing Windows Servers.

However in recent years, PowerShell has gone cross platform and runs most anywhere. It's lightweight and has .NET Core at its, ahem, core. You can use PowerShell for scripting systems on any platform and if you're a .NET developer the team has made installing and immediately using PowerShell in scripts a one liner - which is genius. It's PowerShell as a .NET Global Tool.

Here's an example output from my system running Ubuntu. I just "dotnet tool install --global PowerShell."

$ dotnet --version
2.1.502
$ dotnet tool install --global PowerShell
You can invoke the tool using the following command: pwsh
Tool 'powershell' (version '6.2.2') was successfully installed.
$ pwsh
PowerShell 6.1.1
https://aka.ms/pscore6-docs
Type 'help' to get help.
PS /mnt/c/Users/Scott/Desktop>
exit

Here I've checked that I have .NET 2.x or above, then I install PowerShell. I can run scripts or I can drop into the interactive shell. Note the PS prompt and my current directory above.

In fact, PowerShell is so useful as a scripting language when combined with .NET Core that PowerShell has been included as a global tool within the .NET Core 3.0 Preview Docker images since Preview 4. This means you can use PowerShell lines/scripts inside Docker images.

FROM mcr.microsoft.com/dotnet/core/sdk:3.0
RUN pwsh -c Get-Date
RUN pwsh -c "Get-Module -ListAvailable | Select-Object -Property Name, Path"

Being able to easily install PowerShell as a global tool means you can count on it in your scripts, CI/CDs systems, or docker containers. It's also nice to be able to be able to use existing PowerShell scripts cross platform.

I'm impressed with this idea - installing PowerShell itself as a .NET Global Tool. Very clever and useful.


Sponsor: Ossum unifies agile planning, version control, and continuous integration into a smart platform that saves 3x the time and effort so your team can focus on building their next great product. Sign up free.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Wednesday, July 24, 2019 7:09:37 AM UTC
It is a good option but I would still stick with the chocolatey package management. Assuming it is there, as dotnetcore is as well, just execute

choco install powershell-core -y

The best part is that with just the following all dependencies are updated in the system, including dotnetcore :)
choco upgrade all -y

I need to admit though that chocolatey feels somehow strange with containers so maybe the dotnetcore global option is better suited. I say feels because there is nothing wrong with it, but somehow it seems a bit an overhead in containers.

Alex Sarafian
Wednesday, July 24, 2019 8:37:22 AM UTC
@Alex Sarafian I think the problem with chocolatey is that it's windows only, whereas dotnet global tools (including powershell core) will work on Mac and Linux as well.
Wednesday, July 24, 2019 2:30:50 PM UTC
And once again, a command line tool fails to use the configured Windows proxy settings, so those of us behind a corporate proxy have to fiddle around with looking up our proxy server in the PAC script, putting cleartext passwords into HTTP_PROXY (or is it HTTPS_PROXY?), and crossing our fingers each time.
John
Friday, July 26, 2019 8:39:17 PM UTC
Scott,

A. Can we get a blog entry on the state of Blazor including how the example code in the docs does not have any error handling? Good error handling is 50% of the effort in writing web code

B. Can we get a blog entry on what is in the pipeline for being made open source by Microsoft. Seems as thought 2019 is a dry year with nothing really open sourced outside of the C++ terminal code.

C. Can we get a blog entry on the metrics of what is used in Visual Studio? It's a very large product and with so many features, there are many which are very seldom used.

The VP levels up from my team asked me which of the new features in Visual Studio 2019 were justification for spending the large cost of upgrading, retesting and releasing a new version of our large software systems. He dismissed the UI reskinning, labeled many features as 'micro-features' and was skeptical of even undertaking a Visual Studio 2019 upgrade. "Shouldn't we wait and upgrade to Visual Studio 2021 instead and skip every other large version number Visual Studio?" was his main question. Our organization already skips releases of large database servers due to the cost and business risk.

Robert
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.