Is there a way to make simple Powershell queries of SCCM more efficient?

Joe Emenaker 1 Reputation point
2021-09-01T20:06:38.197+00:00

I'm trying to automate some maintenance of device collections in SCCM. The problem is that even (seemingly) simple commands are kicking off a cascade of redundant queries. For example, just to verify that a certain folder exists (before trying to create a collection in it):

$isItThere = Get-Item -Path "SCCM:\DeviceCollection\Delegated\Dept\Servers"
# I could use Test-Path, but that takes even *longer*

Behold what this yields to the verbose output stream. Marvel at the number of duplicated queries:

Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777730 AND SearchFolder = 0 AND Name = 'Servers'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777730 AND SearchFolder = 0 AND Name = 'Servers'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777730 AND SearchFolder = 0 AND Name = 'Servers'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777730 AND SearchFolder = 0 AND Name = 'Servers'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777730 AND SearchFolder = 0 AND Name = 'Servers'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777730 AND SearchFolder = 0 AND Name = 'Servers'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777730 AND SearchFolder = 0 AND Name = 'Servers'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777730 AND SearchFolder = 0 AND Name = 'Servers'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777730 AND SearchFolder = 0 AND Name = 'Servers'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777730 AND SearchFolder = 0 AND Name = 'Servers'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777730 AND SearchFolder = 0 AND Name = 'Servers'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 0 AND SearchFolder = 0 AND Name = 'Delegated'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777255 AND SearchFolder = 0 AND Name = 'Dept'
Executing WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ObjectTypeName = 'SMS_Collection_Device' AND ParentContainerNodeId = 16777730 AND SearchFolder = 0 AND Name = 'Servers'

Anybody know of a way to keep this from happening? Maybe get the SCCM provider to cache its results or maybe a more-efficient command for verifying SCCM collection folders?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,429 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,461 Reputation points
    2021-09-06T09:17:23.997+00:00

    Hi there,

    Can you have a look at the Below link and see if it is helpful . Basically it is explains about the
    Best practices for collections in Configuration Manager
    https://learn.microsoft.com/en-us/mem/configmgr/core/clients/manage/collections/best-practices-for-collections

    Hope this Answers all your queries , if not please do repost back .
    If an Answer is helpful, please click "Accept Answer" and upvote it : )

    0 comments No comments