How to Create User Collections Based on User Groups in System Center 2012 Configuration Manager SP1

Introduction

This script shows how to create user collections based user groups in System Center 2012 Configuration Manager SP1.

Scenario

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.

Script

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:

  1. Start Windows PowerShell (X86).
  2. Change the directory to <ConfigMgrConsoleInstallationPath>\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin, and then import the Configuration Manager module by typing this command: Import-Module -Name ConfigurationManager.psd1.
  3. Use the CD command in the Windows PowerShell console to change the context of your session to a different site.
  4. Import the module file that contains New-OSCCMCollection.

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.  

PowerShell
Edit|Remove
# 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($_) 
    }}

Examples

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

Additional Resources

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

Forum Threads:
http://social.technet.microsoft.com/Forums/en-US/cb9127f6-f470-4c5a-97fb-e6ad528f81cb/sccm-2012-user-collection-based-on-active-directory-user-group

http://social.technet.microsoft.com/Forums/en-US/59a1defd-5ca3-4607-8c83-027230f3e4f6/sccm-2012-buid-computer-collection-based-on-user-group-membership-primary-user