Create CM Colloction Using PowerShell

I recently had a task to create 20 CM device collections for an application deployment. Instead of creating the collections manually, which would burn my eye, I wrote a PowerShell script to do that for me.

Here is an example of how to make and populate a SCCM machine and user collection.

# Import the ConfigurationManager.psd1 module
Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' 
# Set the current location to be the site code
Set-Location '<sidecode>:' 

# Get current domain
$domain = (Get-ADDomain).Name

# Create SCCM device collection
New-CMDeviceCollection -Name "DIS Training Machine" -Comment "This collection contains all training DIS machines" -LimitingCollectionName "All Systems"

# Create SCCM user collection
New-CMUserCollection -Name "DIS Training User" -Comment "This collection contains all training DIS users" -LimitingCollectionName "All Users"

# Load the list of the machines
$machines = Get-Content C:\DIS_Machines.txt

# Load the list of users
$users = Get-Content C:\DIS_Users.txt

# Add machines to the device collection
foreach($computer in $machines){
Add-CMDeviceCollectionDirectMembershipRule -CollectionName "DIS Training Machine" -ResourceId $(get-cmdevice -Name $computer).ResourceID
}

# Add users to the user collection
foreach($user in $users)
{
Add-CMUserCollectionDirectMembershipRule -CollectionName "DIS Training User" -ResourceId $(Get-CMUser -Name $domain\$user).ResourceID
}

 

Have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *