Create SCCM Device Collections with new-cmdevicecollection

Jason Barrett Jason Barrett | | Device Collection

In this article I will show you how you can use powershell to create device collections in bulk using the New-CMDeviceCollection command in powershell.

I have built a lot of SCCM environments and one thing I do is create around 200 custom device collections.  I have created a powershell scrip which I use to create these device collections which I will share with you today.

I have placed the full script I use at the bottom of this article.

Create-SCCM-Device-Collections-via-Powershell

To Create SCCM Device Collections via Powershell follow these steps

Create The Base Powershell Script

First we will need to create the powershell script which will hold the variables for the device collections we are going to create.

The below script is going to do the following

  • Get the site code and set it to be used in the script
  • Create a “Software Updates” folder. rename or remove this step as required
  • Set the update schedule for the device collection. I recommend you change the time by 1 hour for every collection you create
  1. Open Windows Powershell ISE
  2. Copy the below script and paste in to powershell

    # Set Site Code
    $SiteCode = Get-PSDrive -PSProvider CMSITE
    Set-Location “$($SiteCode.Name):”

    #Create Software Update Folder
    New-Item -Name ‘Software Updates’ -Path “$($SiteCode.Name):\DeviceCollection”

    #Create Schedule
    $ScheduleWeekly = New-CMSchedule -Start “01/01/2017 07:00 PM” -DayOfWeek Sunday -RecurCount 1

    powershell script

  3. The lines 2 and 3 will get the SCCM site code, Do not edit these lines
  4. Line 6 will create a folder called Software Updates in the root of device collections, Rename Software Updates with the folder name you want to create, or remove line 6 if you do not wish to create a folder
  5. Line 9 will set the schedule update variable for the device collection update
  6. Replace 07:00 PM with the time you want the update to happen
  7. You can also change Sunday with the day you want the update to happen

If we run the script as it is the only thing it will do is create the “Software Updates” folder as shown below.

device-collections-software-updates

Create Device Collections Using new-cmdevicecollection

Using the powershell command new-cmdevicecollection we can create SCCM device collections in bulk.

The device collections will need to use one of the following rule types to populate the group.

  • Direct
  • Query
  • Include
  • Exclude

For more information about the different rule types check out this article I wrote.

Below I will now go through each rule type and show you examples on how to create the device collection using New-CMDeviceCollection.

Direct Membership

To create a device collection with direct membership with no members in it follow these steps;

  1. Copy the below code in to powershell ise

    # Set Site Code
    $SiteCode = Get-PSDrive -PSProvider CMSITE
    Set-Location “$($SiteCode.Name):”

    #Create Software Update Folder
    New-Item -Name ‘Software Updates’ -Path “$($SiteCode.Name):\DeviceCollection”

    #Create Schedule
    $ScheduleWeekly = New-CMSchedule -Start “01/01/2017 07:00 PM” -DayOfWeek Sunday -RecurCount 1

    #Create Device Collection “SU 01 | Disable software Updates”
    $NewCollection01 = New-CMDeviceCollection -Name “SU 01 | Disable software Updates” -LimitingCollectionName “All Systems” -RefreshType Both -RefreshSchedule $ScheduleWeekly
    Move-CMObject -FolderPath “.\DeviceCollection\Software Updates” -InputObject $NewCollection01

    device-collection-direct-membership

  2. On line 12 change “SU 01 | Disable software Updates” to the name you want to give the device collection
  3. Change “All Systems” to the limiting collection you wish to use (Such as Windows 10)
  4. The Refresh type is set to both (Incremental and Full updates) or you can set to -RefreshType Periodic (Only Full Updates)
  5. Line 13 will move the device collection to the Software Updates folder, change Software Updates to the folder you require or delete this line if you wish to keep the device collection in the root folder

Now we have created the device collection we can automate populating the collection using powershell.  For steps on how to do this take a look at this article I wrote.

Query Membership

To create a device collection with a query membership follow these steps;

  1. Copy and past the below code in to powershell

    # Set Site Code
    $SiteCode = Get-PSDrive -PSProvider CMSITE
    Set-Location “$($SiteCode.Name):”

    #Create Software Update Folder
    New-Item -Name ‘Software Updates’ -Path “$($SiteCode.Name):\DeviceCollection”

    #Create Schedule
    $ScheduleWeekly = New-CMSchedule -Start “01/01/2017 07:00 PM” -DayOfWeek Sunday -RecurCount 1

    Add-CMDeviceCollectionQueryMembershipRule -CollectionName “SU 01 | Disable software Updates” -QueryExpression “select SMS_R_System.ResourceId, SMS_R_System.ResourceType, SMS_R_System.Name, SMS_R_System.SMSUniqueIdentifier, SMS_R_System.ResourceDomainORWorkgroup, SMS_R_System.Client from SMS_R_System inner join SMS_G_System_TPM on SMS_G_System_TPM.ResourceID = SMS_R_System.ResourceId” -RuleName “TPM Information”

    query membership

  2. On line 11 replace “SU 01 | Disable software Updates” with the device collection name you wish to create
  3. On line 11 replace the query with the query you wish to use. Make sure the query goes inside the two “”
  4. Replace “TPM Information” with the description you wish to give the rule

Include Membership

To add an include device collection membership you first need to create a device collection that uses direct membership or query membership, then add the include rule.

To add an include rule follow these steps;

  1. Copy the below code and past it in to powershell ise

    # Set Site Code
    $SiteCode = Get-PSDrive -PSProvider CMSITE
    Set-Location “$($SiteCode.Name):”

    #Create Software Update Folder
    New-Item -Name ‘Software Updates’ -Path “$($SiteCode.Name):\DeviceCollection”

    #Create Schedule
    $ScheduleWeekly = New-CMSchedule -Start “01/01/2017 07:00 PM” -DayOfWeek Sunday -RecurCount 1

    #Create Device Collection “SU 01 | Disable software Updates”
    $NewCollection01 = New-CMDeviceCollection -Name “SU 01 | Disable software Updates” -LimitingCollectionName “All Systems” -RefreshType Both -RefreshSchedule $ScheduleWeekly

  2. At the bottom of the powershell script add the following line

    Add-CMDeviceCollectionIncludeMembershipRule -CollectionName “SU 01 | Disable software Updates” -IncludeCollectionName “All Windows 10 Devices”

  3. Replace “All Windows 10 Devices” with the device collection you wish to use as the include

    # Set Site Code
    $SiteCode = Get-PSDrive -PSProvider CMSITE
    Set-Location “$($SiteCode.Name):”#Create Software Update Folder
    New-Item -Name ‘Software Updates’ -Path “$($SiteCode.Name):\DeviceCollection”

    #Create Schedule
    $ScheduleWeekly = New-CMSchedule -Start “01/01/2017 07:00 PM” -DayOfWeek Sunday -RecurCount 1

    #Create Device Collection “SU 01 | Disable software Updates”
    $NewCollection01 = New-CMDeviceCollection -Name “SU 01 | Disable software Updates” -LimitingCollectionName “All Systems” -RefreshType Both -RefreshSchedule $ScheduleWeekly

    #Add Device collection “All Windows 10 Devices” to “SU 01 | Disable software Updates” as include rule
    Add-CMDeviceCollectionIncludeMembershipRule -CollectionName “SU 01 | Disable software Updates” -IncludeCollectionName “All Windows 10 Devices”

    include rule

Exclude Membership

To add an exclude membership collection simply replace the line in the previous include seciotn

Add-CMDeviceCollectionIncludeMembershipRule -CollectionName “SU 01 | Disable software Updates” -IncludeCollectionName “All Windows 10 Devices”

with

Add-CMDeviceCollectionExcludeMembershipRule -CollectionName “SU 01 | Disable software Updates” -excludeCollectionName “All Windows 2012 Devices”

How To Bulk Add Devices To Device Collection

To bulk add devices in to a sccm device collection follow these steps

  1. Create a .txt file on your machine and add the devices you wish to add to the device collection
  2. Run the following powershell script replacing C:\Devices.txt with the path to your .txt file and also replace the Collection Name
  3. Get-Content “C:\Devices.txt” | foreach { Add-CMDeviceCollectionDirectMembershipRule -CollectionName “Collection Name” -ResourceID (Get-CMDevice -Name $_).ResourceID }

For a more indepth guide take a look at How To Add & Remove Multiple Devices To SCCM Collection