How To: Import the ConfigMgr Module – $Profile Style

Setting the scene

If you are like me you probably find yourself frequently in a spot where you would like to import the Configuration Manager module to do this one thing. No one wants to open the console and load the ISE. No one wants to have to use the Snippet, I just want to open PowerShell and be able to get the stupid module if I’m allowed!

There are a LOT of solutions for this, you could download a solution from the PowerShell Gallery. However, that isn’t always going to work. Since I like to work from the PowerShell prompt a lot of the time I came up with this little trick a quick adaptation from my “Get-CMModule” function.

Editing the PowerShell Profile

First we need to store this in the PowerShell profile of our ADMIN user account. (As it likely won’t do us any good in our normal users profile) so we can begin by running PowerShell as an Administrator. Now you can of course put in “$PROFILE” and get the profile location and go open it, or we can cheat and put in “Notepad $PROFILE”

If you want to learn more about the PowerShell profiles you can read more here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-6

Adding a PowerShell Function

Doing this will cause (Assuming you’ve not edited this before) – a blank NotePad file to open and it will ask you if you would like to create a file at X location. (Our answer is of course yes). All that’s left now is to add in some code.

function Get-CMModule
#This application gets the configMgr module
{
    [CmdletBinding()]
    param(
    [Parameter(HelpMessage = "Enter the Site Code you would like to connect to" )]
    [string]$SiteCode = "YOURSITECODEHERE",
    [Parameter(HelpMessage = "Enter the name of the site server you would like to connect to")]
    [string]$SiteServer = "YOURSITESERVERHERE"
    )
    Try
    {
        Write-Verbose "Attempting to import SCCM Module"
        #Retrieves the fcnction from ConfigMgr installation path. 
        Import-Module (Join-Path $(Split-Path $ENV:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1) -Verbose:$false
        Write-Verbose "Succesfully imported the SCCM Module"
        $initParams = @{}
        if($null -eq (Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue)) {
            New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $SiteServer @initParams
        }
    }
    Catch
    {
        Throw "Failure to import SCCM Cmdlets."
    } 
}

This code is available on my GitHub Repository found here:

https://github.com/JordanTheITGuy/PowerShell/blob/master/BlogPosts/HowTo%20-%20Import%20the%20ConfigMgr%20Module%20-%20%24Profile%20Style/Get-CMModule.ps1

Once you’ve put the above code and saved it in the profile. Simply re-open PowerShell as an administrator to have the code become available to you!

If you find yourself using this alot you can of course update the parameters to have the default values instead of having to type in your “SiteCode” and your “Site Server” every time.

Thanks for reading!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: