YAML Powershell Install-Module every time?

Pedro Ribeiro 41 Reputation points
2021-03-03T12:41:22.487+00:00

Hi,
I have an pipeline that executes a powershell script. In that script I need to install a module.

### INSTALL MODULES AND LOGIN TO POWER ###
Write-Host "### Installing PBI module..." -ForegroundColor Green;
Install-module -Name "MicrosoftPowerBIMgmt" -AllowClobber -Scope CurrentUser -Verbose

My questions is: it's possible to install the module just once, without having to install every time the script runs?

Thanks.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 48,581 Reputation points
    2021-03-03T15:17:21.623+00:00

    Yes, if you have permissions. PS has a store of all installed modules. When you install a module you specify whether to install per user or per machine. If you install at the machine level, again assuming permissions, then the module need only be installed once. Thereafter if you try to use a command in the module then PS will auto-load the module. Technically the same thing will work with a per-user module.

    What I personally recommend is that you first check to see if the module is installed. If it isn't then install the module. After that you can run normally and PS will load the module. Something like this for PS 7.1 (not tested).

       function EnsureModuleInstalled ( $name ) {  
          $module = Get-InstalledModule -Name $name  
          if ($null -eq $module) {  
             Install-Module -Name $name -Scope CurrentUser -AllowClobber  
          }  
       }  
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Pedro Ribeiro 41 Reputation points
    2021-03-03T17:15:03.803+00:00

    Sorry, but I forget to mention that this script runs in Azure DevOps.


  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,971 Reputation points Microsoft Vendor
    2021-03-04T06:38:17.553+00:00

    Hi,

    Unless you add the "-Force" switch to the Install-Module cmdlet, the module will be skipped by default if it's already installed.
    Install-Module

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments