ConfigMgr 2012 – Retrieving Objects in a Folder with PowerShell

The introduction of folders in the ConfigMgr 2012 console made for a great way to organize content within the ConfigMgr site.  To see what objects might be under any given folder in the console is simple…but what if you want to do it programmatically with PowerShell?

Folders in ConfigMgr can be accessed through the WMI namespace for the site (Root\SMS\Site_XXX) in two classes:

SMS_ObjectContainerNode – Contains the list of all folder objects in the site.  The Name property corresponds to the display name of the folder.

ContainerNode

SMS_ObjectContainerItem – Contains the list of all associations between an object and a folder (ie – folder membership). The InstanceKey property corresponds to the content ID of the object (ex – CollectionID for a Device Collection).

ContainerItem

By leveraging this information along with the ConfigMgr 2012 Powershell module, we can retrieve a list of objects in a folder with a few quick lines of code.  Consider the following example for retrieving all Device Collections in a particular folder:

$SiteCode = ‘PS1′

$FolderName = ‘Test’

Import-Module (Join-Path (Split-Path $env:SMS_ADMIN_UI_PATH –parent) ConfigurationManager.psd1)

set-location ${SiteCode}:

$FolderObj = Get-WmiObject -Class SMS_ObjectContainerNode -Namespace Root\SMS\Site_$SiteCode -filter “Name=’$FolderName‘” | foreach { $_.ContainerNodeID }

Get-WmiObject -Class SMS_ObjectContainerItem -Namespace Root\SMS\Site_$SiteCode -filter “ContainerNodeID=’$FolderObjID‘” | ForEach-Object {

    Get-CMDeviceCollection -CollectionID $_.InstanceKey | foreach { $_.Name }

}

Checking aganist the ConfigMgr console, we can see that it correctly listed the Device Collections in the Test folder:

GetCollectionsInFolder

 

TestCollections

This method can be used for virtually any content type organized in folders.  For example, if I have a series of folders under the Task Sequences node…

TaskSequences

…I can retrieve a list of those folders, and then retrieve the Task Sequences stored in one of the folders.

PS2

Combined with the Move-CMObject cmdlet, you can easily script out massive reorganization of console content with just a small amount of code!

Many thanks to David O’Brien for some pointers on efficiency in the scripting. Check out his website for more great ConfigMgr PowerShell scripts.

 

email

Written by , Posted .