How to install PowerShell modules in a Visual Studio Code devcontainer

I’m doing a fair bit of work lately in VS Code devcontainers. They are an amazing way to create consistent development environments without having to install tools locally.

I ran into an issue building PowerShell Docker images with modules installed (I specifically needed to install Az PowerShell, the Az ResourceGraph PowerShell module and Pester). Simple running install-module $name -force didn’t do the trick. Below is an example of a Dockerfile for a devcontainer with all the right modules installed:

ARG VARIANT=7

FROM mcr.microsoft.com/azure-functions/powershell:3.0-powershell${VARIANT}-core-tools

RUN pwsh -Command Set-PSRepository -Name PSGallery -InstallationPolicy Trusted && \
    pwsh -Command Install-Module -Name Az -Scope AllUsers -Repository PSGallery && \
    pwsh -Command Install-Module -Name Az.ResourceGraph -Scope AllUsers -Repository PSGallery && \
    pwsh -Command Install-Module -Name Pester -Scope AllUsers -Repository PSGallery && \
    pwsh -Command Set-PSRepository -Name PSGallery -InstallationPolicy Untrusted

CMD [ "pwsh" ]

Leave a Reply