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.

3 thoughts on “How to quickly create Collections using New-CMDeviceCollection

  1. This is great but, (there is always a but!) now how do you automatically create a collection based on a text list of computer names?

  2. Pingback: How to add computers listed in a text file to a device collection1E Blogs – Insight and debate on IT Efficiency

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.