A script snippet I use all the time (and which gets a lot of search traffic) is something I wrote in 2020 about exporting AD computers to CSV with Powershell. Along those lines, another cmdlet I use all the time … usually for inventory/asset management … is to obtain collection data from the SCCM/MEMCM/MECM/CM ecosystem.
There's a Built-in Cmdlet for that!
The cmdlet Get-CMCollectionMember
is super simple to use, especially when pointing it at a specific CollectionID
number (which you can see in the management console), like so:
Get-CMCollectionMember -CollectionID MZD000D
This will spit out a bunch of info about the devices in the collection MZD000D
. From there, you can do fun things:
Export some basic device info to CSV
Get-CMCollectionMember -CollectionID MZD000D |
Select-Object Name, SerialNumber, IsActive, MACAddress, LastActiveTime,
LastLogonUser, PrimaryUser, UserName |
Export-CSV .\Path\To\Output.csv -NoTypeInformation -Encoding UTF8
In this case you get a device name, serial number, MAC Address, and user information, which isn't easy/obvious/possible to get directly from AD itself! Simple, and useful for relaying info to others (say a network team or to cross-reference asset assignments).
Export device info from multiple systems (CM and AD) to CSV
Get-CMCollectionMember -CollectionID MZD000D |
Select-Object Name |
ForEach-Object {
Get-ADComputer -Filter 'Name -eq "$_"' -Property * |
Select-Object Name, IPv4Address } |
Export-CSV .\Path\To\Output.csv -NoTypeInformation -Encoding UTF8
In this case it's possible to grab the name of devices in the identified CM collection, but also then grab the IPv4 address that AD has recorded. This isn't super efficient, and could be cleaned up, but I use it once a year to identify and clean up some DHCP records for lab spaces (in this case it's simpler to relay IP addresses than MAC addresses).
That's Just the Beginning!
As the cmdlet name Get-CMCollectionMember
might imply (for folks familiar with Powershell), there are permutations of this name for things like collections themselves (Get-CMCollection
) or how collections are managed (Get-CMDeviceCollectionQueryMembershipRule
). For instance:
Get-CMCollection -Name "MZD - *" |
Foreach-Object {
Get-CMDeviceCollectionQueryMembershipRule -CollectionId $_.CollectionId } |
Foreach-Object { $_.RuleName, $_.QueryExpression } |
Out-File ".\Path\To\CollectionQueryRules.txt" -Encoding UTF8
During a project, I had to move devices within AD to a very different OU path. In order to preserve the CM side of things during that transition, in advance of the actual device migration I needed to modify the collection query rules. Since the majority of my query-based collection rules are for AD OUs, running this gave me a succinct list of all collections with queries (and their query rules). Armed with this info, I could identify, evaluate, and create a plan for how/when to move certain devices. Running this meant I didn't have to click through all the collections or rely on my memory or naming convention. Specifically, with the above I was able to query all the collections that started with the name MZD -
. This saved a load of time and let me know where to look/change queries accordingly.
Always More to Learn
You can see all the Configuration Manager cmdlets and read more at Microsoft Learn. Super useful for random automations or those one-off things that crop up! You never know what might be reusable either!
Good luck, and happy scripting!