How to quickly create Collections using New-CMDeviceCollection

It is probably not everyday that you need to create a bunch of collections in ConfigMgr, however, you might find yourself setting up a new infrastructure, lab environment or just doing some load and performance testing that justifies the need for some automation. Using the technique I demonstrated in my previous post Fun with PowerShell – Formatting Numbers, we can simply add an extra line inside the for loop to do just that.  By formatting the number of digits and using it as part of the collection name, your collections will have a consistent ordering when viewing them in the console.  Here is a quick example that creates 10 collections and places the formatted number at the end of the collection name:

for ($x=1; $x -le 10; $x++){
#Format the number as two digits in order to maintain a consistent look for collection names, etc.
$a="{0:D2}" -f $x
New-CMDeviceCollection -Name "TEST_Collection_$a" -LimitingCollectionName "All Systems"

Depending on your preference, you could also place the number either before the collection name or in the middle of the name.  Just remember to use the backtick (`) as an escape character if using an underscore after your variable.  So, if you wanted to put the number at the beginning of the name, the New-CMDeviceCollection line would look like this:

New-CMDeviceCollection -Name "$a`_TEST_Collection" -LimitingCollectionName "All Systems"

When you need to clean up and delete your test collections, use the Remove-CMDeviceCollection command and place the wildcard asterisk in the place of the digits:

Remove-CMDeviceCollection -Name "TEST_Collection_*" -Force

Automating collection creation is also a good way to enforce naming standards and keep your console neat and organized.



Written by , Posted .

One Comment

  1. Lakshminarayana

    How to set a collection refresh interval of 4 hours when I use a query to get members from an AD security group ?

Leave a Comment

You must be logged in to post a comment.