I know we have migrations tools and other built-in options when we want to build a new ConfigMgr environment. But Microsoft have given us Powershell, and there are some really cool cmdlets that we can utilize.

I’ve had a couple of examples lately where i had to create 100+ collections from scratch – or basicly from just a list of applications… And instead of doing that by hand i would much rather do it with Powershell, and save my poor fingers alot of clicking and typing.

Microsoft have a Technet site where all ConfigMgr 2012 R2 cmdlets are listed and described – http://technet.microsoft.com/en-us/library/jj821831(v=sc.20).aspx

Based on all the info from that Microsoft website i did the following:

1 – Collections need Update Schedules, so i went ahead and defined them to begin with.

$Schedule1 = New-CMSchedule -Start "01/01/2014 9:00 PM" -DayOfWeek Monday -RecurCount 1 
$Schedule2 = New-CMSchedule -Start "01/01/2014 9:00 PM" -DayOfWeek Tuesday -RecurCount 1 
$Schedule3 = New-CMSchedule -Start "01/01/2014 9:00 PM" -DayOfWeek Wednesday -RecurCount 1 
$Schedule4 = New-CMSchedule -Start "01/01/2014 9:00 PM" -DayOfWeek Thursday -RecurCount 1 
$Schedule5 = New-CMSchedule -Start "01/01/2014 9:00 PM" -DayOfWeek Friday -RecurCount 1 
$Schedule6 = New-CMSchedule -Start "01/01/2014 9:00 PM" -DayOfWeek Saturday -RecurCount 1 
$Schedule7 = New-CMSchedule -Start "01/01/2014 9:00 PM" -DayOfWeek Sunday -RecurCount 1

2 – Defined my Device Collections to be created.

New-CMDeviceCollection -Name "CollectionName1" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule1 -RefreshType Periodic 
New-CMDeviceCollection -Name "CollectionName2" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule2 -RefreshType Periodic 
New-CMDeviceCollection -Name "CollectionName3" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule3 -RefreshType Periodic 
New-CMDeviceCollection -Name "CollectionName4" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule4 -RefreshType Periodic 
New-CMDeviceCollection -Name "CollectionName5" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule5 -RefreshType Periodic 
New-CMDeviceCollection -Name "CollectionName6" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule6 -RefreshType Periodic 
New-CMDeviceCollection -Name "CollectionName7" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule7 -RefreshType Periodic

3 – Defined a Query Rule for my Device Collections.

Add-CMDeviceCollectionQueryMembershipRule -CollectionName "CollectionName1" -QueryExpression "select *  from  SMS_R_System where SMS_R_System.SystemGroupName = 'DOMAIN\\SecurityGroup1'" -RuleName "QueryRuleName1" 
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "CollectionName2" -QueryExpression "select *  from  SMS_R_System where SMS_R_System.SystemGroupName = 'DOMAIN\\SecurityGroup2'" -RuleName "QueryRuleName2" 
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "CollectionName3" -QueryExpression "select *  from  SMS_R_System where SMS_R_System.SystemGroupName = 'DOMAIN\\SecurityGroup3'" -RuleName "QueryRuleName3" 
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "CollectionName4" -QueryExpression "select *  from  SMS_R_System where SMS_R_System.SystemGroupName = 'DOMAIN\\SecurityGroup4'" -RuleName "QueryRuleName4" 
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "CollectionName5" -QueryExpression "select *  from  SMS_R_System where SMS_R_System.SystemGroupName = 'DOMAIN\\SecurityGroup5'" -RuleName "QueryRuleName5" 
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "CollectionName6" -QueryExpression "select *  from  SMS_R_System where SMS_R_System.SystemGroupName = 'DOMAIN\\SecurityGroup6'" -RuleName "QueryRuleName6" 
Add-CMDeviceCollectionQueryMembershipRule -CollectionName "CollectionName7" -QueryExpression "select *  from  SMS_R_System where SMS_R_System.SystemGroupName = 'DOMAIN\\SecurityGroup7'" -RuleName "QueryRuleName7"

4 – Defined my User Collections to be created.

New-CMUserCollection -Name "CollectionName1" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule1 -RefreshType Periodic 
New-CMUserCollection -Name "CollectionName2" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule2 -RefreshType Periodic 
New-CMUserCollection -Name "CollectionName3" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule3 -RefreshType Periodic 
New-CMUserCollection -Name "CollectionName4" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule4 -RefreshType Periodic 
New-CMUserCollection -Name "CollectionName5" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule5 -RefreshType Periodic 
New-CMUserCollection -Name "CollectionName6" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule6 -RefreshType Periodic 
New-CMUserCollection -Name "CollectionName7" -LimitingCollectionName "LimitingCollection" -RefreshSchedule $Schedule7 -RefreshType Periodic

5 – Defined a Query Rule for my User Collections.

Add-CMUserCollectionQueryMembershipRule -CollectionName "CollectionName1" -QueryExpression "select *  from  SMS_R_User where SMS_R_User.UserGroupName = 'DOMAIN\\SecurityGroup1'" -RuleName "QueryRuleName1" 
Add-CMUserCollectionQueryMembershipRule -CollectionName "CollectionName2" -QueryExpression "select *  from  SMS_R_User where SMS_R_User.UserGroupName = 'DOMAIN\\SecurityGroup2'" -RuleName "QueryRuleName2" 
Add-CMUserCollectionQueryMembershipRule -CollectionName "CollectionName3" -QueryExpression "select *  from  SMS_R_User where SMS_R_User.UserGroupName = 'DOMAIN\\SecurityGroup3'" -RuleName "QueryRuleName3" 
Add-CMUserCollectionQueryMembershipRule -CollectionName "CollectionName4" -QueryExpression "select *  from  SMS_R_User where SMS_R_User.UserGroupName = 'DOMAIN\\SecurityGroup4'" -RuleName "QueryRuleName4" 
Add-CMUserCollectionQueryMembershipRule -CollectionName "CollectionName5" -QueryExpression "select *  from  SMS_R_User where SMS_R_User.UserGroupName = 'DOMAIN\\SecurityGroup5'" -RuleName "QueryRuleName5" 
Add-CMUserCollectionQueryMembershipRule -CollectionName "CollectionName6" -QueryExpression "select *  from  SMS_R_User where SMS_R_User.UserGroupName = 'DOMAIN\\SecurityGroup6'" -RuleName "QueryRuleName6" 
Add-CMUserCollectionQueryMembershipRule -CollectionName "CollectionName7" -QueryExpression "select *  from  SMS_R_User where SMS_R_User.UserGroupName = 'DOMAIN\\SecurityGroup7'" -RuleName "QueryRuleName7"

Note: If you want incremental updating enabled on the Collections you can set the –RefreshType to Both instead of Periodic

Now there might be some Powershell guru out there saying – pfft i can do that much better by creating some automation and check’s and UI maybe even. But for normal beings like me this might be helpfull to a some.

So feel free to grab the code and save some time.

– ConfigMgr is my high!

Get the Powershell file from my Skydrive: https://skydrive.live.com/redir?resid=9F84E3E619840C84%215704