Last active
March 9, 2026 18:11
-
-
Save RaRaRatchet/c8c4b113be4250db5dc18663fb24412e to your computer and use it in GitHub Desktop.
[Get All DeviceCollections in a Folder ] Get Folder Details and get Collections in a Folder #powershell #ConfigurationManager #SCCM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function Get-SCCMCollectionsInFolder | |
| { | |
| <# | |
| .SYNOPSIS | |
| Returns all the collections located in the specified folder ID. | |
| .DESCRIPTION | |
| Connects to the specified site server and retrieves the details of the specified folder as output that in term can be used for other functions. | |
| This function is usable for Device or User collections any other items will need different WMI queries and these would be best added to a seperate function. | |
| .EXAMPLE | |
| Get-SCCMCollectionsInFolder -FolderID <id of your folder> | |
| .EXAMPLE | |
| Get-SCCMCollectionsInFolder -FolderID <id of your folder> -SiteServer mysiteserver.example.com | |
| .PARAMETER FolderID | |
| This parameter is the folder ID (can be gathered using a different function Get-SCCMFolderDetail) | |
| .PARAMETER FolderType | |
| The FolderType parameter is used to specify the folder type (5000 for Device Collections or 5001 for User Collections) | |
| .PARAMETER SiteServer | |
| The SiteServer parameter contains the name of the site server that can provide the collections contained below. | |
| .PARAMETER SiteCode | |
| The SiteCode parameter is optional and if not provided automatically retrieved from the specified site server. | |
| .PARAMETER Full | |
| This is an optional and determines that all collection fields need to be gathered from the site server this will include member count etc. | |
| #> | |
| Param | |
| ( | |
| [Parameter(Mandatory=$True,ValueFromPipeline=$true)] | |
| [string]$FolderID, | |
| [Parameter(Mandatory=$False)] | |
| [string]$FolderType = "5000", | |
| [Parameter(Mandatory=$False)] | |
| [string]$SiteServer = "mysiteserver.example.com", | |
| [Parameter(Mandatory=$false)] | |
| [string]$SiteCode = (Get-WmiObject -Namespace "root\SMS" -Class SMS_ProviderLocation -ComputerName $SiteServer).SiteCode, | |
| [Parameter(Mandatory=$False)] | |
| [switch]$Full = $false | |
| ) | |
| Begin | |
| { | |
| Write-Verbose "SCCM Site Server : $($SiteServer)" | |
| Write-Verbose "SCCM Site code : $($SiteCode)" | |
| Write-Verbose "SCCM Folder ID : $($FolderID)" | |
| } | |
| Process | |
| { | |
| Switch ($FolderType) | |
| { | |
| "5000" {$SCCMCollectionType = "2"} | |
| "5001" {$SCCMCollectionType = "1"} | |
| default {$SCCMCollectionType = "2"} | |
| } | |
| Write-Verbose "SCCM Collection Type : $($SCCMCollectionType)" | |
| $FolderDetails = Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Query "Select * from SMS_ObjectContainerNode where` | |
| ContainerNodeID='$($FolderID)'" -ComputerName $SiteServer | |
| If ($Full) | |
| { | |
| Write-Verbose $FolderDetails | |
| } | |
| Else | |
| { | |
| Write-Verbose "SCCM Folder Name : $($FolderDetails.Name)" | |
| } | |
| $SCCMCollectionQuery ="select Name,CollectionID from SMS_Collection where CollectionID is in(select InstanceKey from SMS_ObjectContainerItem ` | |
| where ObjectType='$($FolderType)' and ContainerNodeID='$FolderID') and CollectionType='$($SCCMCollectionType)'" | |
| If ($Full) | |
| { | |
| $SCCMCollectionQuery ="select * from SMS_Collection where CollectionID is in(select InstanceKey from SMS_ObjectContainerItem` | |
| where ObjectType='$($FolderType)' and ContainerNodeID='$FolderID') and CollectionType='$($SCCMCollectionType)'" | |
| } | |
| $CollectionsInSpecficFolder = Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Query $SCCMCollectionQuery -ComputerName $SiteServer | |
| If ($VerbosePreference -eq "continue") | |
| { | |
| ForEach ($Collection in $CollectionsInSpecficFolder) | |
| { | |
| Write-Verbose "SCCM Collection Name : $($Collection.name)" | |
| Write-verbose $Collection | |
| } | |
| } | |
| } | |
| End | |
| { | |
| return $CollectionsInSpecficFolder | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function Get-SCCMFolderDetail | |
| { | |
| <# | |
| .SYNOPSIS | |
| Returns the folder details that includes the container ID needed for other functions. | |
| .DESCRIPTION | |
| Connects to the specified site server and retrieves the details of the specified folder as output that in term can be used for other functions. | |
| By default the Device Folder type is specified unless overridden using the FolderType parameter, you can specify the following folder types: | |
| Type 2 : Package | |
| Type 3 : Advertisement | |
| Type 9 : Software Metering | |
| Type 18 : OS Images | |
| Type 19 : Boot Images | |
| Type 20 : Task Sequences | |
| Type 23 : Drivers | |
| Type 2011 : Configuration Baselines | |
| Type 5000 : Device Collection | |
| Type 5001 : User Collection | |
| Type 6000 : Application | |
| Type 6001 : Configuration Items | |
| .EXAMPLE | |
| Get-SCCMFolderDetail -FolderName "Maintenance Collections" | |
| .EXAMPLE | |
| Get-SCCMFolderDetail -FolderName "Maintenance Collections" -SiteServer mysiteserver.example.com | |
| .PARAMETER FolderName | |
| The FolderName parameter is the name of the folder for which you want to gather the details. | |
| .PARAMETER SiteServer | |
| The SiteServer parameter contains the name of the site server that can provide the folder details. | |
| .PARAMETER FolderType | |
| The FolderType parameter is optional and set to Device Folder by default unless specified (see description) | |
| .PARAMETER SiteCode | |
| The SiteCode parameter is optional and if not provided automatically retrieved from the specified site server. | |
| #> | |
| Param | |
| ( | |
| [Parameter(Mandatory=$true)] | |
| [string]$FolderName, | |
| [Parameter(Mandatory=$true)] | |
| [string]$SiteServer, | |
| [Parameter(Mandatory=$false)] | |
| [string]$FolderType = "5000", # This is the device collection folder type | |
| [Parameter(Mandatory=$false)] | |
| [string]$SiteCode = (Get-WmiObject -Namespace "root\SMS" -Class SMS_ProviderLocation -ComputerName $SiteServer).SiteCode | |
| ) | |
| Begin | |
| { | |
| Write-Verbose "SCCM Site Server : $($SiteServer)" | |
| Write-Verbose "SCCM Site Code : $($SiteCode)" | |
| Write-Verbose "SCCM Folder Name : $($FolderName)" | |
| Write-Verbose "SCCM Folder Type : $($FolderType)" | |
| } | |
| Process | |
| { | |
| $FolderDetails = Get-wmiObject -Namespace "root\SMS\site_$($SiteCode)" -Query "Select name,containernodeid,objecttype,objecttypename from SMS_ObjectContainerNode where name = '$($FolderName)' AND objecttype = '$($FolderType)'" -ComputerName $SiteServer | |
| } | |
| End | |
| { | |
| return $FolderDetails | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment