PowerShell and Configuration Manager 2012 R2–Part 2


Summary: Use the Configuration Manager cmdlets to work with site collections.

Hey, Scripting Guy! Question Hey, Scripting Guy!

I deal with Configuration Manager on a regular basis, and I have to manage User and Device collections. I’d like an easier way to add and remove members and audit the memberships of site collections. Could you show me how to do that with the cmdlets in Configuration Manager?

—RL

A: Hello RL,

Honorary Scripting Guy, Sean Kearney, is here to further delve into some basic uses of those ever so magical Configuration Manager cmdlets.

   Note  This is a five-part series that includes the following posts:

I remember having to work with collections with Configuration Manager 2007, and I wished that there was an easier way.

To be honest, there was. It involved VBScript, WMI, and…well, it was akin to chewing on foil. I didn’t enjoy the process.

Then they provided Configuration Manager cmdlets in Windows PowerShell—and oh, feint! It got so much better!

Now finding the cmdlets is actually pretty easy. The naming convention for many of the aspects of Configuration Manager reflects the cmdlet name. To identify all the cmdlets that work with collections, I can type:

Get-Command –module ConfigurationManager *Collection*

I ran a quick count. 53 cmdlets are available to handle collections! That seems like a lot to work with. But if you split that between User and Device, you’re about 26. Then break that down into cmdlets that get information and various other verbs, and the list is really not that scary.

If I simply want a list of all the cmdlets that can get a collection, I can filter further:

Get-Command –module ConfigurationManager Get*Collection*

This provides a much more manageable list to start with.

Image of command output

We can already see cmdlets for accessing the User and Device collections. If we initially run something like Get-CMDeviceCollection, the output appears long and messy, and it is not well formatted. We can place the output in something more viewable with Format-Table:

Get-CMDeviceCollection | Format-Table

Image of command output

It is more useful to the eyes, but it doesn’t really look like the output from the console, does it? We can mitigate this with Select-Object and grab some key properties, like with any other PowerShell cmdlet:

Get-CMDeviceCollection | Select-Object CollectionID, Comment, LocalMemberCount, LastRefreshTime

Image of command output

If you’d like to identify direct memberships in a particular Device collection, you can provide the name of a Device collection and use the Get-CMDeviceCollectionDirectMembershipRule cmdlet (now that's a mouthful!):

Get-CMDeviceCollectionDirectMembershipRule –CollectionName ‘Test Deployment Group’

Or if you’d like to see which Include queries exist, you can ask that question just as easily. For example, if you were curious as to the query being used to pull down the list of systems in ‘QA Systems’, you would use this line:

Get-CMDeviceCollectionQuery –CollectionName ‘QA Systems’

We can even easily remove or add memberships with the cmdlets (whether they are Include-, Exclude-, Direct-, or Query-based). We can even create brand new ones on the fly with the New-CMDeviceCollection cmdlet:

New-CMDeviceCollection –Name ‘HSG Collection’ –LimitingCollectionName ‘All Systems’

Or as an example, I can totally remove a Device collection. Here, I remove the Device collection I just created:

Remove-CMDeviceCollection –Name ‘HSG Collection’

Now if we do this, we will get a warning to confirm if we’d like to perform this destructive task. If you really don’t like to be asked the question, you can force it through. (Please remember before you do this, don't practice on the production server!)

Remove-CMDeviceCollection –Name ‘HSG Collection’ –confirm:$False -force

Working with User collections is very similar with the exception that the cmdlet will have the word User in it, and of course, you are targeting users, not devices.

So put your mind to work on what you need to do on a regular basis with your collections in Configuration Manager.

RL, there is an introduction to working with collections with the Configuration Manager cmdlets. Tomorrow I'll work with distribution points and the Configuration Manager cmdlets.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then always remember that with great PowerShell comes great responsibility.

Sean Kearney, Honorary Scripting Guy and Cloud and Datacenter Management MVP

Comments (0)

Skip to main content