How To Add & Remove Multiple Devices To SCCM Collection

Jason Barrett Jason Barrett | | Device Collection

There are many different ways you can quickly add multiple devices to a SCCM collection, The one I use the most is by using powershell.

Below I will show you three ways I quickly add devices to collections in SCCM.

How To Add Multiple Devices To SCCM Collection

There are three ways that I add multiple devices to sccm collections which are

  1. Via Powershell
  2. Via Add Resources and mass select and add
  3. Query rule

Each method had its advantages and limitations which I will cover below.

Powershell

Adding devices with powershell is the method I use the most often.  To add multiple devices to collections using powershell follow these steps

    1. Log on to your SCCM server
    2. Open the configuration manager console
    3. Go to \Assets and Compliance\Overview\Device Collections
    4. Copy the name of the collection you want to add to (In my example I am going to use the Test Collection which has 0 members)
      test device collection
    5. Create a folder on your SCCM server, I have created C:\Scripts
    6. Create two files in this folder, Devices.txt and SCCM_Add_Devices_To_Collection.ps1
      create folder and files
    7. In the devices.txt file, add the devices you want to add to the device collection
      add devices to devices txt file
    8. Edit the SCCM_Add_Devices_To_Collection.ps1 file with powershell and add the below code
      1
      2
      $SiteCode = Get-PSDrive -PSProvider CMSITE
      Set-Location "$($SiteCode.Name):"$Devicelist = (get-content "C:\Scripts\Devices.txt")$DeviceCollection = "Test Collection"$Devicelist | foreach { Add-CMDeviceCollectionDirectMembershipRule -CollectionName $DeviceCollection -ResourceID (Get-CMDevice -Name $_).ResourceID }
    9. Replace the text Test Collection with the name of the collection you want to add devices to
    10. Also replace C:\Scripts\Devices.txt if you created this file in a different folder
    11. Run the powershell script two times, The first time you run the script it will fail because its not set the path correctly
    12. Once run you will see no output
      run powershell script to add devices to collections
    13. Go to Go to \Assets and Compliance\Overview\Device Collections
    14. Locate your collection, right click on it and select refresh
    15. You should now see the member count go up
      device collection with added members

If you added the wrong devices to a device collection you can remove them in bulk as well, to bulk remove devices take a look at this section

Via Add Resources

Another method I use is to search for a device name, then highlight the required devices and add them to the collection.

To do this follow these steps.

  1. Open the configuration manager console
  2. Go to Go to \Assets and Compliance\Overview\Device Collections
  3. Right click on the collection you want to add to
  4. Click on Add Resources
    add resources to device collection
  5. In the name string contains enter part of the device name you want to search for, Below I searched for VM
  6. Click the search button
  7. Left click and highlight the devices you want to add
  8. Click Add
  9. Confirm the devices under selected resources
  10. Click ok
    add resources
  11. The devices will now be added to the device collection

Query Rule

By using a query rule you can automatically add devices that meet certain criteria such as

How To Remove Multiple Devices From SCCM Collection

There are a few different ways you can bulk remove multiple devices from sccm collections which I will go through below.

Bulk Remove All Members From A Collection

Using the script below will enable you to remove all direct memberships from the selected device collection.

To do this follow these steps

  1. Log on to the SCCM server
  2. Edit the below powershell script replacing “Test Collection” with the name of the collection you want to remove all the direct members from
    1
    2
    3
    $SiteCode = Get-PSDrive -PSProvider CMSITE
    Set-Location "$($SiteCode.Name):"
    Get-CMDeviceCollectionDirectMembershipRule -CollectionName 'Test Collection' | Select RuleName,ResourceID| ForEach-Object {Remove-CMDeviceCollectionDirectMembershipRule -CollectionName 'Test Collection' -ResourceID $_.ResourceID -Force}
  3. Open powershell and paste the updated script in to it
  4. Run the script
  5. Manually check the device collection to make sure the devices have been removed

Bulk Remove Select Members From A Collection

This is the removal script I use the most.  You can put a list of devices or users in to a .txt file then use powershell to remove these resources from the collection.

To do this follow these steps

  1. Log on to your SCCM server
  2. Open the configuration manager console
  3. Go to \Assets and Compliance\Overview\Device Collections
  4. Copy the name of the collection you want to remove members from
  5. Create a folder on your SCCM server, I have created C:\Scripts
  6. Create two files in this folder, Devices.txt and SCCM_Remove_Devices_From_Collection.ps1
  7. In the devices.txt file, add the devices you want to remove from the device collection
  8. Edit the SCCM_Remove_Devices_From_Collection.ps1 file with powershell and add the below code
    1
    2
    3
    $SiteCode = Get-PSDrive -PSProvider CMSITE
    Set-Location "$($SiteCode.Name):"$Devicelist = (get-content "C:\Scripts\Devices.txt")
    $DeviceCollection = "Test Collection"$Devicelist | foreach { Remove-CMDeviceCollectionDirectMembershipRule -CollectionName $DeviceCollection -ResourceID (Get-CMDevice -Name $_).ResourceID -Force }
  9. Replace the text Test Collection with the name of the collection you want to remove devices from
  10. Also replace C:\Scripts\Devices.txt if you created this file in a different folder
  11. Run the powershell script two times, The first time you run the script it will fail because its not set the path correctly
  12. Once run you will see no output
  13. Go to Go to \Assets and Compliance\Overview\Device Collections
  14. Locate your collection, right click on it and select refresh
  15. You should now see the member count go down

Bulk Remove Query Rules From Collections

Using powershell it is also possible to remove query rules from device collections.

To do this run the below command in powershell on the SCCM server

Remove-CMDeviceCollectionQueryMembershipRule -CollectionName “Test Collection” -RuleName “ZZZQuery” -Force

  • Replace Test Collection with the name of the collection you want to remove the rule from
  • Replace ZZZQuery with the name of the query rule

 

Leave a Comment