This script shows how to create user collections based user groups in System Center 2012 Configuration Manager SP1.
Currently, you often need to create user collections based on user groups in Active Directory. After that, you can deploy applications and packages for specific users. Usually an Active Directory contains a lot of user groups, but it's a time consuming task to create collections based on user groups in SCCM console.
So you need to use a script to reduce your workload.
This script module contains the following advanced function:
Please use this script in a Windows PowerShell session that has the Configuration Manager module imported. To use the Configuration Manager module in a Windows PowerShell session:
Additionally, it’s recommended to use this script in a SCCM environment with latest cumulative update installed.
Here are some code snippets for your reference.
# Create collections
if ($PSCmdlet.ShouldProcess($newColName))
{
try
{
$newCol = New-CMUserCollection -Name $newColName `
-LimitingCollectionName $LimitingCollectionName -RefreshSchedule $cmSchedule
Add-CMUserCollectionQueryMembershipRule -Collection $newCol `
-RuleName $newColName -QueryExpression $memberRule
}
catch
{
$PSCmdlet.ThrowTerminatingError($_)
}
}
# Create collections if ($PSCmdlet.ShouldProcess($newColName)) {try{ $newCol = New-CMUserCollection -Name $newColName ` -LimitingCollectionName $LimitingCollectionName -RefreshSchedule $cmSchedule Add-CMUserCollectionQueryMembershipRule -Collection $newCol ` -RuleName $newColName -QueryExpression $memberRule }catch{ $PSCmdlet.ThrowTerminatingError($_) }}
Example 1: How to display help about New-OSCCMCollection.
To display help about this function, please run the following command.
Get-Help New-OSCCMCollection -Full

Example 2: How to show that user collections would be created.
To show how collections would be created, please run the following command.
New-OSCCMCollection -Filter "Ala%" -Verbose -WhatIf

Note It’s highly recommended to run this example each time before actually creating the collections.
Example 3: How to create user collections based on the user groups in Active Directory. If a name conflict occurs, the existing collection will be removed.
To create collections for users based on the user groups in Active Directory. If a name conflict occurs, the existing collection will be removed. Please run the following command.
New-OSCCMCollection -Filter "Ala%" -Verbose -Force

Example 4: How to create user collections based on the user groups in Active Directory.
To create user collections based on the user groups in Active Directory and add prefix in collection name, please run the following command.
New-OSCCMCollection -Filter "Ala%" -NamePrefix "All users in user group" -Verbose

Technical Resources:
Windows PowerShell Advanced Function
SMS_R_UserGroup Server WMI Class
New-CMUserCollection
Add-CMUserCollectionQueryMembershipRule