A lot of scripts (VBScript, PowerShell, or otherwise) which depend on loading the ConfigurationManager.psd1 module, seem to assume the default path is where it always lives.  Quite often however, engineers will install System Center Configuration Manager (aka ConfigMgr, aka SCCM, aka “kitchen sink full of automation dishes”) in a custom location.  There are at least two methods to find the actual location via PowerShell (or VBScript, actually).

1 – Fast

The fastest route to execute this query is by using a direct registry query against a known (and consistent) key path and value…

function Get-CmAdminConsolePath-Fast {
  $result = Get-ItemProperty "HKLM:SOFTWARE\Microsoft\SMS\Setup" | 
    Select-Object -ExpandProperty "UI Installation Directory"
  $cpath = "$result\bin\ConfigurationManager.psd1"
  if (Test-Path $cpath) { $cpath }

2 – Slow

This one harkens back to my older blog days (sigh…) which uses the CIM_Datafile logical store to query for matching file data.  But I’ve updated it for PowerShell.  Warning: This method is exponentially SLOWER than a direct registry query, but that’s because where performing a search rather than a directed read operation:

function Get-CmAdminConsolePath-Slow {
  Get-CIMInstance CIM_Datafile `
    -Filter "extension='psd1' AND name LIKE '%\\configurationmanager%'" |
      Select-Object -ExpandProperty Name

3 – Cheap

This is a bit of a hail-Mary toss, but if the console is installed, it should have a corresponding shortcut in the All-Users profile start folder.  So we lean back and do a sneaky reach-around on the COM “Wscript.Shell” class to use an ancient Chinese method of reading a shortcut’s properties.  It worked for Confucius, so it’s good enough for me…

function Get-CmAdminConsolePath-Cheap {
  $ShortCut = $env:PROGRAMDATA+"\Microsoft\Windows\Start Menu\Programs\Microsoft System Center\Configuration Manager\Configuration Manager Console.lnk"
  if (Test-Path $ShortCut) {
    $sh = New-Object -COM WScript.Shell
    $targetPath = $sh.CreateShortcut($ShortCut).TargetPath
    $result = $targetpath.Replace('Microsoft.ConfigurationManagement.exe','ConfigurationManager.psd1')

And more?  There’s probably at least a half-dozen alternatives to these two, and I don’t doubt that any/all of them could be more effective.  But here’s two to start with.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s