The many ways to import the ConfigMgr cmdlet Library module

Over the years I’ve seen and used a variety of PowerShell commands to get the ConfigurationManager.psd1  file location to pass to Import-Module

Just for fun, I compiled a list of the different methods.  It’s all about string manipulation.

Import-Module "$env:SMS_ADMIN_UI_PATH\..\configurationmanager.psd1" # shortest code
Import-Module $env:SMS_ADMIN_UI_PATH.Replace('\bin\i386','\bin\configurationmanager.psd1') # most readable code
Import-Module $env:SMS_ADMIN_UI_PATH.Replace('i386','configurationmanager.psd1')
Import-Module $env:SMS_ADMIN_UI_PATH -replace 'i386$','configurationmanager.psd1'
Import-Module "$($env:SMS_ADMIN_UI_PATH.TrimEnd('i386'))configurationmanager.psd1"
Import-Module ((Split-Path $env:SMS_ADMIN_UI_PATH)+'\configurationmanager.psd1')
Import-Module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5)+'\configurationmanager.psd1')

When using a ConfigMgr cmdlet it is necessary to work from the ConfigMgr site’s drive.  I’ve also seen a variety of ways to accomplish this as detailed below.

$SiteCode = Get-PSDrive -PSProvider CMSITE
Push-Location "$($SiteCode.Name):\"

$SiteCode = (Get-PSDrive -PSProvider CMSITE).Name
Push-Location "$($SiteCode):\"
Push-Location "$SiteCode`:\"

Push-Location "$(Get-PSDrive -PSProvider CMSITE).Name):\"

When more than one ConfigMgr site is registered on a computer, it may be necessary to more specifically identify it.  This is one example.

$SiteCode = (Get-PSDrive -PSProvider CMSITE | Where {$_.Root -eq 'ConfigMgr.contoso.com'}).Name

Lastly, note the difference between the *-Location  cmdlets.  Using Push/Pop-Location allows changing to the ConfigMgr site drive before running the cmdlets then returning to the previous location afterward.

Push-Location and the alias pushd.

Pop-Location  and the alias popd.

Set-Location  and the aliases cd , chdir , and  sl.

One Response

  1. Trevor March 17, 2017

Leave a Reply to Trevor Cancel reply