Function New-SCCMUserCollection {
PARAM (
[Parameter(Mandatory = $true)] $ADCollectionName,
[Parameter(Mandatory = $true)] $CMCollectionName,
$limitingCollection = “All Users”,
Customize the $path appropriately for your environment.
$path = “OU=Application Deployment,OU=Groups,DC=,DC=”,
$description = “Application Deployment Group”,
Customize your domain name in the query.
$queryExpression = “select SMS_R_USER.ResourceID,SMS_R_USER.ResourceType,SMS_R_USER.Name,SMS_R_USER.UniqueUserName,SMS_R_USER.WindowsNTDomain from SMS_R_User where SMS_R_User.SecurityGroupName = ‘\$($ADCollectionName)’”,
$ServerName = “”,
$SiteCode = “<YourSiteCode”
)
## Get a reference object for grabbing the RefreshSchedule property
# Use an existing collection as a template to replicate the RefreshSchedule.
$refreshSchedule = (Get-CMDeviceCollection -Name “7zip”).RefreshSchedule[0]
$ruleName = $CMCollectionName
## Create AD Security Group
New-ADGroup -Name $ADCollectionName -SamAccountName $ADCollectionName -GroupCategory Security -GroupScope Universal -DisplayName $ADCollectionName -Path $path -Description $description -Verbose
## Create SCCM Collection
New-CMUserCollection -Name $CMCollectionName -LimitingCollectionName $limitingCollection -Verbose -RefreshSchedule $refreshSchedule -RefreshType 2
## Add collection rule
$CollectionName = Get-CMUserCollection -Name $CMCollectionName
Add-CMUserCollectionQueryMembershipRule -CollectionId $CollectionName.CollectionID -RuleName $ruleName -QueryExpression $queryExpression -Verbose
}