This basically does the same as clicking on 'file' in the configMgr admin console.
a lot of times I need to fetch data from the SCCM server and other locations such as Active Directory or a file server.
Description: Run this script on a server that has the configuration manager
this script will load the CMDLets and will set the location to
for more information on the available CmdLets...
Usage: powershell -NoExit -File Connect-CMSite.ps1
# Check the environment variable set by the configuration manager console
if ( $Env:SMS_ADMIN_UI_PATH -eq $null )
throw "Env not found"
#import the ConfigMgr Module
ipmo -Name "$(split-path $Env:SMS_ADMIN_UI_PATH)\ConfigurationManager.psd1"
# try to get the site name
$Site = ( gdr -PSProvider CMSite -ErrorAction SilentlyContinue );
# make sure that the site is valid
if ( ! ( $site ))
Throw "CMSite provider not found"
# Write-Host "Connecting to: $Site"
$CMSite = ( Get-CMSite ).SiteName
Write-Verbose -Message "`n`tWelcome to $CMSite `n"
Adding this code in my script will allow me to connect to the site and use the ConfigMgr CmdLets
0 blog comments below