Add Multiple Devices To SCCM Collection Using PowerShell

Home » Add Multiple Devices to SCCM collection using PowerShell

Add Multiple Devices to SCCM collection using PowerShell

Written By

Prajwal Desai

Last Updated

March 2, 2020

Posted In

Using PowerShell you can easily bulk add multiple devices to SCCM device collection. Using this guide, you will be able to import multiple devices into a target device collection using ConfigMgr PowerShell module.

There could be situations when you have to add a few hundred devices to a collection in SCCM. If it was a single device, you would use a directly query. However when you have got lot of computers, adding each of them would take a long time.

Thanks to PowerShell because it is a much easier and faster option to bulk add devices to SCCM device collection. We will accomplish our task using Add-CMDeviceCollectionDirectMembershipRule which adds a Direct Rule membership rule to a device collection. More info here.

Install and Update Third Party Applications with Patch My PC
Install and Update Third Party Applications with Patch My PC

Step 1 – Create a device collection in SCCM

  • Open the SCCM Console. Go to Assets and Compliance > Overview > Device Collections.
  • Click Create Device Collection in the top ribbon.
  • The Create Device Collection Wizard opens. Specify a name of the new device collection and select the limiting collection. Click Next.

create device collection in sccm

On the Membership Rules page, don’t add any rule. Click Next.

Define membership rules for the collection

When you create a device collection without adding any resources, you get a warning. This collection has no membership rules… Click OK since we will be importing multiple devices into this device collection later.

Add Multiple Devices to SCCM collection using PowerShell Snap3

On the Summary page, click Close.

Summary

On the Completion page, click Close.

Completion

Step 2 – Prepare the Computers List

After creating the target device collection, prepare a TXT file that contains all devices that you wish to add to device collection. In this example, the file name is List_computers.txt and contains the following devices.

Add Multiple Devices to SCCM collection using PowerShell

Step 3 – Import the Computers using ConfigMgr Powershell cmdlets

On the top ribbon of the ConfigMgr console, click to the blue arrow and click Connect via Windows PowerShell.

Add Multiple Devices to SCCM collection using PowerShell Snap7

Copy and paste the below command and don’t forget to change the text file location and the target collection name. Execute the command.

Get-Content "E:\Sources\Import\List_computers.txt" | foreach { Add-CMDeviceCollectionDirectMembershipRule -CollectionName "Computer List" -ResourceID (Get-CMDevice -Name $_).ResourceID }

Add Multiple Devices to SCCM collection using PowerShellAfter few minutes (It depends of the computers number), the device collection import task should be complete. You will find all the devices added to the device collection in SCCM.

Still Need Help?

If you need further assistance on the above article or want to discuss other technical issues, check out some of these options.

Forums Telegram Contact Me

Post navigation

Previous PreviousConfigMgr Technical Preview 2002.2 New FeaturesNextContinue Install SCCM Software Update Point Role | ConfigMgr SUP

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comment *

Name *

Email *

Save my name, email, and website in this browser for the next time I comment.

Δ

25 Comments

  1. Getting the below error ………………….

    Get-CMUser : The term ‘Get-CMUser’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:158 + … rshipRule -CollectionName “User List” -ResourceID (Get-CMUser -Name $ … + ~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-CMUser:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

    Get-CMUser : The term ‘Get-CMUser’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:158 + … rshipRule -CollectionName “User List” -ResourceID (Get-CMUser -Name $ … + ~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-CMUser:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

    Reply
  2. Hey Praj ; I don’t know why this isn’t working exactly. But here’s what i’m getting.

    Add-CMUserCollectionDirectMembershipRule : Cannot bind argument to parameter ‘ResourceId’ because it is null. At line:1 char:129 + … tionName “Server List” -ResourceID (Get-CMUser -Name $_).ResourceID } + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Add-CMUserCollectionDirectMembershipRule], ParameterBindingValidationE xception + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.ConfigurationManagement.PowerSh ell.Cmdlets.Collections.AddUserCollectionDirectMembershipRule

    I’m referencing my Server list.txt ; the servers are just listed by name. What am I missing?

    Reply
  3. anyone get this error:

    Add-CMDeviceCollectionDirectMembershipRule : No object corresponds to the specified parameters. At line:5 char:1 + Add-CMDeviceCollectionDirectMembershipRule -CollectionName “RY1-Remov … + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Microsoft.Confi…tMembershipRule:AddDeviceCollectionDirectMembershipRule) [Add-CMDeviceCol…tMembershipRule], ItemNotFoundException + FullyQualifiedErrorId : ItemNotFound,Microsoft.ConfigurationManagement.PowerShell.Cmdlets.Collections.AddDeviceCollectionDirectMembershipRule

    please help me.

    Reply
  4. hello,

    when i try to import some users see what i have :

    Get-CMDevice : This command cannot be run from the current drive. To run this command you must first connect to a Configuration Manager drive. At line:1 char:177 + … odification Profil Chrome” -ResourceID (Get-CMDevice -Name $_).Resour …

    + CategoryInfo : DeviceError: (Microsoft.Confi…tions.GetDevice:GetDevice) [Get-CMDevice], InvalidOperationException + FullyQualifiedErrorId : CommandCannotExecuteFromCurrentDrive,Microsoft.ConfigurationManagement.PowerShell.Cmdlets.Collections.GetDevice

    i don’t understand because i import the file from the server … can you help me with ?

    thanks a lot

    Reply
  5. Heres a little improvement that checks 1st if the resource exists.

    Get-Content “D:\Support\source\machines.txt” | foreach { write-host “Adding: $_” $device_add = Get-CMDevice -Name $_ if ($device_add ) { Add-CMDeviceCollectionDirectMembershipRule -CollectionName “My_Collection” -ResourceID (Get-CMDevice -Name $_).ResourceID } else { write-host “$_ dont exist in SCCM” } }

    Reply
  6. Thanks Prajwal for sharing the script. I have successfully used it multiple times in my organization to create device collections in bulk.

    Just have one query, once we are done with PowerShell script execution we will be getting the exact device number for collection. I have observed that there is change (Increase/Decrease) in collection devices after few days, and it keep on increasing and decreasing. As per my understanding as this collection is Static, the number should not change.

    Reply
  7. Has anyone gotten this error? Add-CMDeviceCollectionDirectMembershipRule : Cannot bind argument to parameter ‘ResourceId’ because it is null. At line:1 char:146 + … alo Alto 10 percent” -ResourceID (Get-CMDevice -Name $_).ResourceID } + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Add-CMDeviceCol…tMembershipRule], ParameterBindingValidationExceptio n + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.ConfigurationManagement.PowerSh ell.Cmdlets.Collections.AddDeviceCollectionDirectMembershipRule

    Reply
    1. Cause the objects are not exist in your directory.

      Reply
  8. Your articles are always useful and easy to understand. Thank you for sharing.

    Reply
  9. If servers/systems need to be added or removed from the list, is it as simple as updating the .txt file, or will it also require executing the powershell command again as well?

    Reply
  10. Thank you for the command, it worked and saved lot of time.

    Reply
  11. Thank you for the quick and easy solution, works as expected.

    Reply
  12. It worked for me after adding a parameter after for Get-CMDevice. Get-Content “xxx.txt” | foreach { Add-CMDeviceCollectionDirectMembershi pRule -CollectionName “xxx” -ResourceID (Get-CMDevice -Name $_ -Resource).ResourceID}

    Reply
    1. Thank you! Kept getting an error until I added the -Resource 🙂

      Reply
  13. Just use the free “Right Click Tools” software for SCCM/MECM, is so much faster, easier and effective for this action and several others 😉

    Reply
    1. Damn, I didn’t even know, that they had this function! Thx a lot

      Reply
  14. nervermind i foud it =) just change a few params. thanks

    Get-Content “E:\Sources\Import\List_users.txt” | foreach { Add-CMUserCollectionDirectMembershipRule -CollectionName “User List” -ResourceID (Get-CMUser -Name $_).ResourceID }

    Reply
  15. Hi thanks for this script, real time saving . do you know a way to do the same with user collections?

    thank you

    Reply
  16. Pure genius! Thanks so much!

    Reply
  17. New to sccm and packaging deployments. Following the step by step. Not sure how this would solution would work. I”m get the following error when running the cmd:

    Add-CMDeviceCollectionDirectMembershipRule : Cannot bind argument to parameter ‘ResourceId’ because it is null. At line:1 char:144 + … ame “Sophos Machines” -ResourceID (Get-CMDevice -Name $_).ResourceID} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Add-CMDeviceCol…tMembershipRule], ParameterBindingValidationExceptio n + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.ConfigurationManagement.Cmdlets .Collections.Commands.AddDeviceCollectionDirectMembershipRuleCommand

    Seems to me if we are trying to populate my sccm Collection with my machines listed in the .txt file, how would Get-CMDevice know about the device to get a ResourceID?

    Reply
    1. Hey, try importing SCCM modules. “Get-CMDevice” these command sccm specific commands

      Reply
  18. I have faced this before but i can’t recall how i’ve come through. Could anyone help?

    Reply
  19. not working. TXT is referenced correctly. Error Output: something like this:

    Tunrs out the quotation marks in the article above do not like copy pasty nor the copy cmdlet of the html webpage.

    -ResourceID (Get-CMDevice -Name $_).ResourceID } ForEach-Object : Cannot bind parameter ‘RemainingScripts’. Cannot convert the “Get-Content” value of type “System.String” to type “System.Management.Automation.ScriptBlock”. At line:1 char:53 + … Alle.txt” | foreach { Add-CMDeviceCollectionDirectMembershipRule -Col … + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [ForEach-Object], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand

    Reply
    1. Correction: Working, after I replaced the Quotations Marks that came via the copy cmdlet of this webpage to my clipboard. No idea why they copied over wrong. Also, looking correct in powershell, but beeing the wrong characters. Thanks for this solution.

      Reply
      1. This works perfectly!! Thanks

        Reply
  • Facebook
  • X
  • YouTube
  • LinkedIn
  • Pinterest

Prajwal Desai

Prajwal Desai is a highly accomplished technology expert and an 11-time Dual Microsoft MVP (Most Valuable Professional), specializing in Microsoft Intune, SCCM, Windows 365, Enterprise Mobility, and Windows. As a renowned author, speaker, and community leader, he is widely recognized for sharing his in-depth expertise and insights through his blog, YouTube channel, conferences, webinars, and other platforms.

Recast Sponsored AD

Prajwal Desai Recast Banner

Patch My PC Sponsored AD

Tag » Add Computer To Sccm Powershell