Anytime you’re working with multiple objects its always a good idea to try and streamline the process. In this article I’m going to show you how to add multiple computers to SCCM collection using Powershell as well as make an effort to try to keep everything in the command line. Let’s say we want to gather a group of Windows 10 devices that need to be patched. I’m very well aware that we can accomplish this task using a query based collection, but what the hey, there might be other scenarios that we’ll need to get the list from a text file or maybe within a cluster from VMware.
Connect To SCCM Powershell
If you’re doing this on a computer that’s NOT the SCCM site server, you’re going to need to install the SCCM admin console so you can install the binaries needed to connect. However, let’s take look at the different paths for different machine types.
SCCM Site Server PSD1 Module Location:
(Assuming the install dir is on the E Drive)
Windows 10 PSD1 Module Location:
(Under the default install location)
Once you’ve imported the SCCM module you can now change directory into the Primary Site Code.
cd PAC:\
Create Device Collection Using Powershell
In our example we’re going to create a device collection for Windows 10 that needs to be patched. Since we’re now on our way to becoming Powershell Wizards we’ll also create the collection in Powershell.
PS PAC:\> New-CMDeviceCollection -Name "Windows 10 Patch Devices" -LimitingCollectionName "All Systems"
Add Multiple Computers To SCCM Collection Using Powershell
To add a device to a group we’ll leverage the Add-CMDeviceCollectionDirectMembershipRule cmdlet from the SCCM module. Next up we’ll run the little code snippet to execute the job.
foreach ($Computer in $ComputerList) { Add-CMDeviceCollectionDirectMembershipRule -CollectionName "Windows 10 Patch Devices" -ResourceID (Get-CMDevice -Name $Computer).ResourceID }
That’s it! The great thing is that we can reuse that code to add more and more machines as needed. I hope this article was able to help you with your SCCM administration as much as it helped it.
Finally, don’t forget to check out our SCCM Playlist on YouTube for more SCCM tips and tools.