Connecting in to SCCM via PowerShell

It’s easy to launch an interactive PowerShell window by launching the SCCM Console and clicking “Connect via Windows PowerShell”

But it’s kind of a pain in the butt when you want to use the PowerShell ISE or make an automated script that needs to connect in.

Here’s a quick function you can drop in to any script or the ISE script pane to get you up and running:
Function Connect-SCCM {
[string]$SCCMCmdlet = "${env:ProgramFiles(x86)}\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1"
if (!(Test-Path -Path $SCCMCmdlet -PathType Leaf)) { Write-Error "ConfigurationManager.psd1 not found. Make sure the Configuration Manager Console is installed" -ErrorAction Stop } #Make sure module exists
if (!(Get-Module -Name ConfigurationManager)) { Import-Module $SCCMCmdlet } #Import module if not already loaded
Set-Location ((Get-PSDrive -PSProvider CMSite | Select-Object -First 1).Name + ":") #Change directory to the first site detected