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
- Via Powershell
- Via Add Resources and mass select and add
- 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
-
- Log on to your SCCM server
- Open the configuration manager console
- Go to \Assets and Compliance\Overview\Device Collections
- 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)
- Create a folder on your SCCM server, I have created C:\Scripts
- Create two files in this folder, Devices.txt and SCCM_Add_Devices_To_Collection.ps1
- In the devices.txt file, add the devices you want to add to the device collection
- 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 } - Replace the text Test Collection with the name of the collection you want to add devices to
- Also replace C:\Scripts\Devices.txt if you created this file in a different folder
- Run the powershell script two times, The first time you run the script it will fail because its not set the path correctly
- Once run you will see no output
- Go to Go to \Assets and Compliance\Overview\Device Collections
- Locate your collection, right click on it and select refresh
- You should now see the member count go up
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.
- Open the configuration manager console
- Go to Go to \Assets and Compliance\Overview\Device Collections
- Right click on the collection you want to add to
- Click on Add Resources
- In the name string contains enter part of the device name you want to search for, Below I searched for VM
- Click the search button
- Left click and highlight the devices you want to add
- Click Add
- Confirm the devices under selected resources
- Click ok
- 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
- Log on to the SCCM server
- 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} - Open powershell and paste the updated script in to it
- Run the script
- 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
- Log on to your SCCM server
- Open the configuration manager console
- Go to \Assets and Compliance\Overview\Device Collections
- Copy the name of the collection you want to remove members from
- Create a folder on your SCCM server, I have created C:\Scripts
- Create two files in this folder, Devices.txt and SCCM_Remove_Devices_From_Collection.ps1
- In the devices.txt file, add the devices you want to remove from the device collection
- 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 } - Replace the text Test Collection with the name of the collection you want to remove devices from
- Also replace C:\Scripts\Devices.txt if you created this file in a different folder
- Run the powershell script two times, The first time you run the script it will fail because its not set the path correctly
- Once run you will see no output
- Go to Go to \Assets and Compliance\Overview\Device Collections
- Locate your collection, right click on it and select refresh
- 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