I accidentally blew up my dev lab and now I need to build a new one but luckily we have PowerShell :).

In this picture we have 8 Queries and I would like to create Device Collections based on these Queries. These Queries are built with Servicing Extension.

Queries 

Here are some of the commands that we can use in Configuration Manager 2012 R2 CU4. These commands allows us easily export/import Queries from one system to another and much more.

#Import Module
Import-Module $env:SMS_ADMIN_UI_PATH.Replace("\bin\i386","\bin\configurationmanager.psd1")
$SiteCode = Get-PSDrive -PSProvider CMSITE
Set-Location "$($SiteCode.Name):\"

#Get all CM Queries
$Queries = Get-CMQuery
#Total Queries
$Queries.Count

#Export Queries to a CSV file
$Queries | Export-Csv -Path 'D:\Scripts\CMQueries.csv' -NoTypeInformation

#Get only specific CM Queries keyword
$Queries = Get-CMQuery -Name "*ConfigMgr clients *" -ForceWildcardHandling -Verbose

#Total Queries with 'ConfigMgr clients' keyword - 9 in this case
$Queries.Count

#Create new Device Collection folder
New-Item -Path "$($SiteCode.Name):\DeviceCollection" -Name 'System'

#Build Device Collections based on these Queries
foreach($Query in $Queries){

    #Exclude built-in Queries
    If($Query.QueryID.Contains($SiteCode.Name)){
        
        #Create Device Collection
        $Collection = New-CMDeviceCollection -Name $Query.Name -LimitingCollectionName 'All Systems'
        
        #Add Device Collection Query Membership Rule
        Add-CMDeviceCollectionQueryMembershipRule -Collection $Collection -QueryExpression $Query.Expression -RuleName $Query.Name
        
        #Move the Collections to correct folder
        Move-CMObject -InputObject $Collection -FolderPath "$($SiteCode.Name):\DeviceCollection\System"

    }
}

#Print out these new Device Collections
$Collections = Get-CMDeviceCollection -Name "*ConfigMgr clients *"
$Collections

#Export out new Device Collections to a CSV file
$OutPut = @()
foreach($Col in $Collections){
    
    $Object = New-Object -TypeName PSObject -Property @{ 
        CollectionName = $Col.Name 
        QueryName = $Col.CollectionRules.RuleName
        QueryExperssion = $Col.CollectionRules.QueryExpression
    } 

    $OutPut += $Object
}
$OutPut | Export-Csv -Path 'D:\Scripts\CMCollectionsWithQueries.csv' -NoTypeInformation

#Import Device Collections in a new environment
$NewCollections = Import-Csv -Path 'D:\Scripts\CMCollectionsWithQueries.csv'
foreach($COl in $NewCollections){

        #Create Device Collection
        $Collection = New-CMDeviceCollection -Name $COl.CollectionName -LimitingCollectionName 'All Systems'
        
        #Add Device Collection Query Membership Rule
        Add-CMDeviceCollectionQueryMembershipRule -Collection $Collection -QueryExpression $COl.QueryExperssion -RuleName $COl.QueryName
        
}

#If you dont want to create Collections, then just create Queries
$Queries = Import-Csv -Path 'D:\Scripts\CMCollectionsWithQueries.csv'
foreach($Query in $Queries){
    #Create new Query
    New-CMQuery -Expression $Query.QueryExperssion -Name $Query.QueryName -TargetClassName 'SMS_R_System'

}

#Print out the Collections member count
$Collections = Get-CMDeviceCollection -Name "*ConfigMgr clients *"
$Collections | Select-Object -Property Name,LocalMemberCount | Sort-Object -Property LocalMemberCount | Out-GridView


#Invoke Query to get the Devices
Invoke-CMQuery -Name 'ConfigMgr clients at or above version 5.00.7958.1401 (Cumulative Update 3 for System Center 2012 R2 Configuration Manager)'

Little bit later we have nice Device Collections in System folder

 Device_Collections

Using the PowerShell GridView output we can easily explore the Collection membership count

Collection_MemberCount

 

PS – My next Configuration Manager 2012 R2 Automation with PowerShell is in June @LabCenter

 

Happy Scripting