Introducing PowerShell as .NET Global Tool

Aditya Patwardhan

PowerShell is very suitable for CI/CD scenarios due to its easy and well understood scripting paradigm, and its cross-platform support makes it great for building and testing cross-platform applications. A .NET Global Tool is a special NuGet package that contains a console application.

A .NET Core application can be developed for various platforms like Windows, various distributions of Linux and macOS, while the same PowerShell scripts can be used for building, testing and deployment across all platforms.

Installing PowerShell Global tool

If you already have the .NET Core SDK installed, it’s easy to install PowerShell as a .NET global tool!

dotnet tool install --global PowerShell

Once installed, you can run it with pwsh.

PowerShell in .NET SDK docker containers

PowerShell has already been included as a global tool within the .NET Core 3.0 Preview Docker images since Preview.4. These images are a great starting point for building a .NET Core CI/CD image (you can find some awesome samples over at the dotnet-docker repo.)

Docker files with PowerShell syntax

As PowerShell comes pre-installed, Docker files can have PowerShell syntax. This allows you to run scripts or cmdlets as part of your Docker file.

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"

Build scenarios in Docker

In addition to enabling PowerShell syntax, PowerShell scripts in the container can be easily invoked through Docker:

docker run -it -v c:\myrepo:/myrepo -w /myrepo mcr.microsoft.com/dotnet/core/sdk:3.0 pwsh ./build.ps1

The NuGet package for the global tool can be found at: https://www.nuget.org/packages/PowerShell/

Please report issues or suggestions at: https://github.com/PowerShell/PowerShell/issues/new/choose

Thank you!

Aditya Patwardhan Senior Software Engineer PowerShell Team @adityapatward13

3 comments

Discussion is closed. Login to edit/delete existing comments.

  • Joe Browne 0

    Could you please talk about why someone would want to install PowerShell this way, instead of getting the bits from https://github.com/PowerShell/PowerShell/releases ?

  • Brian Bauer 0

    I too have the same question as Joe Browne…  Can you help elaborate as to what this brings to CI/CD or dotnetcore that you can’t do already with Posh or Posh core?

  • Carsten DuellmannMicrosoft employee 0

    Just adding that with the SHELL instruction the above Dockerfile is more readable and concise. Plus we save the detour via bash (see https://docs.docker.com/v17.09/engine/reference/builder/#shell):

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

Feedback usabilla icon