SCCM 2012 Sp1 PowerShell 3 Adding Computers to Collection from File

Hello and greetings to Henry’s blog …

Hi I’m Yitzhak Bohadana and a friend of Henry,

I was playing today with SCCM 2012 SP1, i came a cross a need to add Cross Forest Migration computers to a  new Collection that will be deployed an Antivirus later.

Like all-ways i looked up a way to Automate the processes with are beloved Powershell 3.0.

I found out  that the internet doesn’t supply a  simple explanation how to do it within a few easy steps, so i decide to post a simple way to accomplished this goal.

So here we go…

First you have to have Powershell 3.0 (no skipping !)

System Requirements:

Windows Management Framework 3.0 requires Microsoft .NET Framework 4.0. You can install Microsoft .NET Framework 4.0 at

(You can get full INFO here) install here is the script for auto-installing via CMD (Original Microsoft Source HERE)

For windows 7 / Server 2008R2

dism /online /enable-feature:NetFx2-ServerCore
dism /online /enable-feature:MicrosoftWindowsPowerShell
dism /online /enable-feature:NetFx2-ServerCore-WOW64

Now You good to go

First you Open :

Go to start and open the SCCM Console hit ALT amd Click the Connect via Windows PowerShell

Now you need to  agree the license (Like we all-ways do …)

To add one Computer to the Collection use this script :

$CollectionName = "Test"

$DeviceName = "Comp123"

Add-CMDeviceCollectionDirectMembershipRule -CollectionName "$CollectionName" -ResourceID $(get-cmdevice -name "$DeviceName").ResourceID

To add multiple Computers from TXT file use this script:

$ErrorActionPreference= 'silentlycontinue'

$CollectionName = "Test1"

$Computers = get-content C:\computers.txt

Foreach ($Computer in $Computers)

{ add-cmdevicecollectiondirectmembershiprule -collectionname $CollectionName -resourceid (Get-CMDevice -name $Computer).ResourceID }

Now you have a bulk script from file with computers names

I  used $ErrorActionPreference option with “silentlycontinue”  which means it will hide the two common errors.

Note !! In general if the computer is already in the collection : You will receive the next error “computer is already a member of this collection”you can remove the ErrorActionPreference to see more errors like  computers  that not reported to the sscm database yet. This computers are with no ResourceID there for you will see ResourceID is null in red error on the powershell meaning  that this computer is not in the database of the SCCM .An example for this kind of error :

“add-cmdevicecollectiondirectmembershiprule : Rule named \"Computer1\" already exists in the specified collection”

Add-CMDeviceCollectionDirectMembershipRule : Cannot validate argument on parameter 'ResourceId'. The argument is null


I suggest to add it with a script, or wait for the next push installation of sccm, more options is to manually install the client or use a startup script with GPO (Group policy)

To use a script follow this post :

To use GPO follow this post :

Thanks for reading

Yitzhak Bohadana


2 thoughts on “SCCM 2012 Sp1 PowerShell 3 Adding Computers to Collection from File

  1. Thanks so much for the article.Much thanks again. Great. bdcdbfcdbaca

  2. Thanks a bunch! This was helpful for me. I’m going to make a post on my blog and kick it back to you!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s