Close Menu
    Facebook X (Twitter) Instagram
    Saturday, October 11
    X (Twitter) LinkedIn Reddit RSS
    All about Endpoint Management
    • Home
    All about Endpoint Management
    Home»CM2012»SCCM Configmgr Remove Collection membership for Direct rule Collections using Powershell

    SCCM Configmgr Remove Collection membership for Direct rule Collections using Powershell

    Eswar KonetiBy Eswar KonetiJanuary 05, 1:16 pm5 Mins Read CM2012 9,206 Views
    Share
    Facebook Twitter LinkedIn Reddit

     

    This blog post is continuation to my previous post ‘Monitor collection evaluation's and remove incremental membership schedule for non-priority collections’ .More information can be found at http://eskonr.com/2019/01/sccm-configmgr-monitoring-collection-evaluations-and-change-update-membership-schedule-using-powershell/ .

    In this post ,we will see how to improve the collection evaluation performance further by identifying list of collections with direct rule created that have membership (incremental and/or full update) enabled and use powershell to remove the membership schedule.

    we will use the powershell script to identify 1)Direct based collections with Scheduled updates 2) Direct based collections with incremental updates + schedule updates 3) Direct based collections with incremental updates and remove the membership schedule

    You can use different rules to configure the members of a collection in SCCM like Direct Rule ,Query rule, include and exclude. For more information on collection types, please refer https://docs.microsoft.com/en-us/sccm/core/clients/manage/collections/create-collections

    Direct Rule: This is a static collection (manual changes required all the time) which means the membership does not change unless you remove a resource from SCCM.

    Query Rule:This is dynamic collection and it dynamically update the membership of a collection based on a query that SCCM runs on a schedule.

    image

    As you see in the above snapshot ,the type of rule is Direct for direct (static) based collections and for query based collections,you will see Query in-place of direct.

    Direct rule collections do not require the membership enabled because these collections are static and they never get update again and again unless user do manual changes .

    If you are adding the resources to direct based collections using scripts then make sure you also use the syntax  to update the collection membership right there in the script .This is required for direct based collections if you did not enable the incremental or full schedule update.

    Why is it required to update collection membership if you use script to add resources direct based collection (consider no membership enabled ) ? 

    If you are adding the resources manually to the collection using GUI then it does refresh the collection automatically and get the resources into the collection for you where as through scripting ,it doesn't get the resources but just that ,the resources will be only added to the collection (GUI as i shown above) but if you open the collection ,it will be empty (this is what i noticed in my testing with scripting).

    With this ,we now need to identify the collections that are direct based which have schedule membership enabled (incremental or/and full update) and remove the membership using powershell script.

    Microsoft recommendation is Do not use incremental updates for a large number of collections. This configuration might cause evaluation delays when you enable it for many collections. The threshold is about 200 collections in your hierarchy. For more info refer here

    Following is simple powershell script to query direct based collections with membership schedule enabled.

    we are using built-in SCCM powershell cmdlet get-CMcollection to get all collections (user and device based) that have membership enabled (SMS_CollectionRuleDirect).

    This script will save the list of direct based collections with schedule enabled to CSV file for reference at later stage. If you have any direct based collections to exclude from this ,you can use script that i posted in previous blog.

    Depends on your infra and number of collections you have ,it might take sometime . For me ,it took 4 min to get 700+ collections that fall in the criteria.

    Note: Before you run the script in production ,make sure you understand the requirement and also comment the $Collection.Put() so you can verify the list of collections you have infra and rerun the script by un-comment the line.

    <#
    Title: Update membership schedule for collections with direct based rule. Direct rule based collections do not need membership enabled.
    Following are the collection membership values for refreshtype
    1:No Scheduled Update
    2:Full Scheduled Update
    4:Incremental Update (Only)
    6:Incremental and Full Update Scheduled
    Author: Eswar Koneti
    Blog:www.eskonr.com
    Date:31-12-2018
    #>

    $scriptPath = $script:MyInvocation.MyCommand.Path
    $CD = Split-Path $scriptpath #Get the current directory of the script that is located
    $RefreshTypeto='1' #This is to convert the membership schedule ,1 is to remove the schedule.
    $date = (get-date -f dd-MM-yyyy-hhmmss) #Get the current date and time when script runs
    $collectionsfound="$CD\collections with direct rules-"+$date+".csv"
    #This is our output file to pipe all collections with direct based rules for our reference later.

    $ErrorActionPreference= 'silentlycontinue'

    #Load SCCM module and map the powershell drive
    Try
    {
      import-module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)  #Import the powershell module . Make sure you have SCCM console installed on the PC that you run the script .
      $SiteCode=Get-PSDrive -PSProvider CMSITE #Get the sitecode
      cd ((Get-PSDrive -PSProvider CMSite).Name + ':')
    }
    Catch
    {
      Write-Host "[ERROR]`t SCCM Module couldn't be loaded. Script will stop!"
      Exit 1
    }
    #Get all collections with membership enabled and direct membership rule only and export the collection details to CSV file for reference
    get-CMcollection | where-object {$_.RefreshType -in ('2','4','6') -and ($_.Properties.CollectionRules.SmsProviderObjectPath -eq "SMS_CollectionRuleDirect")} `
    |  select collectionID,Name | Export-CSV -NoTypeInformation $collectionsfound -append

    foreach ($Coll in Import-Csv $collectionsfound ) #start the for loop for each each collection that found in SCCM and remove the collection membership schedule
    {
    $Collection = Get-CMCollection -CollectionId $Coll.collectionID
    #write-host $Coll.collectionID $Coll.Name
      $Collection.RefreshType = $RefreshTypeto
      $Collection.Put()
    }
    write-host "Execution of script completed:" -foregroundcolor Yellow

    you can also download the script from here

    Following is the SQL code to pre-check and post-check the collections with membership schedule enabled.

    select coll.SiteID,coll.CollectionName,
    case when coll.CollectionType='1' then 'User' else 'Device' end as 'Collection Type'
    from v_Collections_G coll
    where coll.SiteID not in (select CRQ.collectionid from v_CollectionRuleQuery CRQ)
    and coll.Flags in ('2','4','6')
    group by coll.SiteID,coll.CollectionName,coll.CollectionType

     

    Hope you enjoyed reading this article. See you in next post!

    collection membership CollectionRules configmgr direct based collections direct query rule Get-CMCollection Powershell SCCM SMS_CollectionRuleDirect SQL wmi
    Share. Twitter LinkedIn Email Facebook Reddit

    Related Posts

    Export Microsoft Entra ID User Authentication Methods to CSV using PowerShell & Microsoft Graph API

    August 13, 2:08 pm

    SCCM SQL Report – Compare Installed Apps on TWO Different Computers

    July 13, 10:35 am

    Exporting Intune Win32 Apps with All Properties Using PowerShell and Microsoft Graph

    June 30, 7:01 pm

    4 Comments

    1. Pingback: SCCM Technical preview version 1901 – Management insight rules for collections | Eswar Koneti Blog

    2. Pingback: Remove Recurring Schedules from Device Collections in SCCM Before Upgrading to 1810

    3. Madhu Sunke on January 7, 2019 7:47 PM

      in some cases collections may have both query and direct membership rules and just modified your script to pull only Direct membership rule defined in collections and exclude if collection is having both query and direct membership rules.

      #Get all collections with membership enabled and direct membership rule only and export the collection details to CSV file for reference
      $colM = get-CMcollection | where-object {$_.RefreshType -in ('2','4','6') }
      #-and ($_.Properties.CollectionRules.SmsProviderObjectPath -ne "SMS_CollectionRuleQuery")} `
      #| select collectionID,Name | Export-CSV -NoTypeInformation $collectionsfound -append
      $arm = @()
      foreach($co in $colM)
      {
      $Count = ((($co.Properties.CollectionRules.SmsProviderObjectPath) | select -Unique)).count
      if($Count -gt 1){continue}
      elseif($co.Properties.CollectionRules.SmsProviderObjectPath -eq 'SMS_CollectionRuleDirect'){$arm+=$co}
      }

      $arm | select collectionID,Name | Export-CSV -NoTypeInformation $collectionsfound -append

      Reply
      • Eswar Koneti on January 9, 2019 6:16 PM

        Thanks for it, it helps others.

        Regards,
        Eswar

        Reply

    Leave a ReplyCancel reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Sign Up

    Get email notifications for new posts.

    Author

    I’m Eswar Koneti ,a tech enthusiast, security advocate, and your guide to Microsoft Intune and Modern Device Management. My goal? To turn complex tech into actionable insights for a streamlined management experience. Let’s navigate this journey together!

    Support

    Awards

    Archives

    © Copyright 2009-2025 Eswar Koneti, All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.