Creating ConfigMgr Device Collections with PowerShell

Below for reference a quick PowerShell sample for creating a query-based device collection in System Center Configuration Manager (ConfigMgr), along with a demonstration of some of the options you can supply to control collection population and refresh behavior.

You can download the up-to-date ConfigMgr PowerShell cmdlets at

The ConfigMgr PowerShell cmdlet reference documentation is available at

Sample script for device collections

# Import ConfigMgr PowerShell module 
Import-Module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1) 

# Change to ConfigMgr drive, where HOU is your site code
cd ‘c:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin’
Set-Location HOU:

# Create schedules for refreshing collection membership
$MonSched = New-CMSchedule -Start "01/01/2017 10:00 PM" -DayOfWeek Monday -RecurCount 1
$TueSched = New-CMSchedule -Start "01/01/2017 10:00 PM" -DayOfWeek Tuesday -RecurCount 1
$WedSched = New-CMSchedule -Start "01/01/2017 10:00 PM" -DayOfWeek Wednesday -RecurCount 1
$ThuSched = New-CMSchedule -Start "01/01/2017 10:00 PM" -DayOfWeek Thursday -RecurCount 1
$FriSched = New-CMSchedule -Start "01/01/2017 10:00 PM" -DayOfWeek Friday -RecurCount 1
$SatSched = New-CMSchedule -Start "01/01/2017 10:00 PM" -DayOfWeek Saturday -RecurCount 1
$SunSched = New-CMSchedule -Start "01/01/2017 10:00 PM" -DayOfWeek Sunday -RecurCount 1

# Define device collection we will create 
# If you want to enable incremental updating on the Collections, you can set the –RefreshType to 'Both'. 
New-CMDeviceCollection -Name "Pete Servers" -LimitingCollectionName "All Systems" `
-RefreshSchedule $MonSched -RefreshType Periodic

# Define query rule for my collection 
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "Pete Servers" `
-QueryExpression "select * from SMS_R_System where SMS_R_System.SystemGroupName = 'CONTOSOCORP\\Pete Servers'" `
-RuleName "PeteServerQueryRule"

Questions or comments? Use the comments section below.

Leave a Reply