ConfigMgr migration script – Change limiting collections

Posted on Updated on

When doing a migration of ConfigMgr, it could be handy to bulk convert collections that were migrated and need a new limiting collection. The below code example checks the last modified date of the collection … and if it was changed “today” (date when the script executes), then it will set the collection to have a new limiting collection. In this example, it was setting the limiting collection to one that I had created for All Windows Workstation Clients.


# Site code of primary (e.g. GAL:)
Set-Location "GAL:"

# Increase the max number of collection as follows
Set-CMQueryResultMaximum 300

# Collection ID of the new limiting collection
$LimitingCollection = "GAL00003"

# Build array of all collections
$CollArray = Get-CMDeviceCollection

# Get today's date to convert collections created TODAY
# If needing to do date other than TODAY, then set static string value as MM/DD/YYYY
$Today = get-date -format d
Write-Host "Today's date: " $Today

ForEach ($Collection in $CollArray){
# Get collection ID and creation date
$CollID = $collection.CollectionID
$CollDate = $Collection.LastChangeTime.Date.ToShortDateString()

If ($CollID.StartsWith("SMS")) {
# No need to convert "out of box" collections
Write-Host "Skipped SMS Pkg ID: " $CollID -f DarkYellow
} Else {
If ($CollDate.Equals($Today)) {
If ($Collection.LimitToCollectionID.Equals($LimitingCollection)) {
# Already converted if limiting collection already matches
Write-Host "Limiting collection already set for: " $Collection.Name

} Else {
# Date match of collection with today to be converted
Set-CMDeviceCollection -CollectionId $Collection.CollectionID -LimitingCollectionId $LimitingCollection
Write-Host "Set new limiting collection for: " $Collection.Name -ForegroundColor Green
} Else {
# Dates that do not match today should not be converted
Write-Host "Mismatch date: " $CollDate " for " $Collection.Name -f DarkRed

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s