Below for reference a quick PowerShell sample for creating a query-based device collection in System Center Configuration Manager (ConfigMgr), as well as a new folder for organizing collections in the ConfigMgr Admin console. The script then moves the new collection to the new folder. Additionally, there is 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 https://www.microsoft.com/en-us/download/details.aspx?id=46681.
The ConfigMgr PowerShell cmdlet reference documentation is available at https://technet.microsoft.com/en-us/library/jj821831(v=sc.20).aspx.
Sample script for device collections
# Import ConfigMgr PowerShell module Import-Module $env:SMS_ADMIN_UI_PATH.Replace("\bin\i386","\bin\configurationmanager.psd1") # Change to ConfigMgr drive, where HOU is your site code $SiteCode = Get-PSDrive -PSProvider CMSITE Set-Location "$($SiteCode.Name):" # Create schedule for refreshing collection membership $MonSched = New-CMSchedule -Start "01/01/2017 10:00 PM" -DayOfWeek Monday -RecurCount 1 # Create a new admin folder New-Item -Name 'Server Collections' -Path "$($SiteCode.Name):\DeviceCollection" # 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" #Get my new collection by name $MyCollection = Get-CMDeviceCollection -Name 'Pete Servers' # Move my new collection to the new admin folder Move-CMObject -InputObject $MyCollection -FolderPath "$($SiteCode.Name):\DeviceCollection\Server Collections"
Questions or comments? Use the comments section below.