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.
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
- Open Windows Powershell ISE
- 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 - The lines 2 and 3 will get the SCCM site code, Do not edit these lines
- 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
- Line 9 will set the schedule update variable for the device collection update
- Replace 07:00 PM with the time you want the update to happen
- 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.
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;
- 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 - On line 12 change “SU 01 | Disable software Updates” to the name you want to give the device collection
- Change “All Systems” to the limiting collection you wish to use (Such as Windows 10)
- The Refresh type is set to both (Incremental and Full updates) or you can set to -RefreshType Periodic (Only Full Updates)
- 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;
- 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 1Add-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”
- On line 11 replace “SU 01 | Disable software Updates” with the device collection name you wish to create
- On line 11 replace the query with the query you wish to use. Make sure the query goes inside the two “”
- 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;
- 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 - At the bottom of the powershell script add the following line
Add-CMDeviceCollectionIncludeMembershipRule -CollectionName “SU 01 | Disable software Updates” -IncludeCollectionName “All Windows 10 Devices”
- 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”
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
- Create a .txt file on your machine and add the devices you wish to add to the device collection
- Run the following powershell script replacing C:\Devices.txt with the path to your .txt file and also replace the Collection Name
-
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