SOLVED

add computers to security group automatically

%3CLINGO-SUB%20id%3D%22lingo-sub-2081342%22%20slang%3D%22en-US%22%3Eadd%20computers%20to%20security%20group%20automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2081342%22%20slang%3D%22en-US%22%3E%3CP%3EI%20would%20like%20to%20add%20computers%20in%20AD%20with%20names%20that%20start%20with%20desktop%20to%20a%20security%20group%3A%20testgroup.%3C%2FP%3E%3CP%3EWe%20would%20like%20to%20run%20this%20powershell%20command%20thru%20scheduled%20tasks%20to%20run%20every%20week%20so%20that%20if%20someone%20forgets%20to%20add%20the%20computer%20to%20the%20security%20group%20it%20will%20be%20done.%20Every%20computer%20with%20name%20desktop%20must%20be%20aded%20to%20that%20group.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20the%20following%3A%3C%2FP%3E%3CP%3EGet-ADComputer%20-Filter%20'Name%20-like%20%22desktop*%22'%20-properties%20displayname%20%7C%20add-adgroupmember%20-identity%20%22testgroup%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20i%20run%20this%20it%20asks%20for%20a%20members(0)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20can%20we%20accomplish%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20like%20to%20first%20test%20it%20with%20desktop1%20in%20stead%20of%20*.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2081342%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eadd%20computers%20to%20security%20group%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2081924%22%20slang%3D%22en-US%22%3ERe%3A%20add%20computers%20to%20security%20group%20automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2081924%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F605036%22%20target%3D%22_blank%22%3E%40Surfer10%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUse%20the%20following%3C%2FP%3E%3CP%3E(Get-ADComputer%20-Filter%20'Name%20-like%20%22Desktop*%22'%20-properties%20displayname).foreach%7Badd-adgroupmember%20-identity%20%22MyADGROUP%22%20-Members%20%24_.SamAccountName%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20remember%20that%20computer%20object%20should%20have%20at%20their%20end%20%24%2C%20otherwise%20it%20will%20be%20considered%20as%20ad%20user%20account.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E--------------%3C%2FP%3E%3CP%3EIf%20you%20find%20this%20answer%20helpfull%20%2C%20Click%20on%20best%20response%20and%20give%20like%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2082025%22%20slang%3D%22en-US%22%3ERe%3A%20add%20computers%20to%20security%20group%20automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2082025%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%20this%20is%20what%20i%20get%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMethod%20invocation%20failed%20because%20%5BMicrosoft.ActiveDirectory.Management.ADComputer%5D%20does%20not%20contain%20a%20method%20named%20'foreac%3CBR%20%2F%3Eh'.%3CBR%20%2F%3EAt%20line%3A1%20char%3A1%3CBR%20%2F%3E%2B%20(Get-ADComputer%20-Filter%20'Name%20-like%20%22desktop1*%22'%20-properties%20displayname%20...%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20InvalidOperation%3A%20(foreach%3AString)%20%5B%5D%2C%20RuntimeException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20MethodNotFound%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMust%20there%20be%20a%20%24%20at%20desktop1%24%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2082252%22%20slang%3D%22en-US%22%3ERe%3A%20add%20computers%20to%20security%20group%20automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2082252%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F605036%22%20target%3D%22_blank%22%3E%40Surfer10%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20i%20put%20%24%20ehind%20the%20computername%20the%20error%20does%20not%20appear%20but%20it%20does%20also%20not%20adding%20the%20computer%20to%20the%20security%20group%2C%20just%20tested%20with%20%22desktop1%24%22%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2082266%22%20slang%3D%22en-US%22%3ERe%3A%20add%20computers%20to%20security%20group%20automatically%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2082266%22%20slang%3D%22en-US%22%3E%3CP%3EWhen%20i%20do%20this%3A%3C%2FP%3E%3CP%3EGet-ADComputer%20-Filter%20'Name%20-like%20%22desktop1%22'%20-properties%20displayname%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20properties%20of%20the%20computer%20appears%20so%20thats%20okay.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F605036%22%20target%3D%22_blank%22%3E%40Surfer10%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I would like to add computers in AD with names that start with desktop to a security group: testgroup.

We would like to run this powershell command thru scheduled tasks to run every week so that if someone forgets to add the computer to the security group it will be done. Every computer with name desktop must be aded to that group.

 

I have the following:

Get-ADComputer -Filter 'Name -like "desktop*"' -properties displayname | add-adgroupmember -identity "testgroup"

 

When i run this it asks for a members(0)

 

How can we accomplish this?

 

 

 

 

I would like to first test it with desktop1 in stead of *.

11 Replies

@Surfer10 

Use the following

(Get-ADComputer -Filter 'Name -like "Desktop*"' -properties displayname).foreach{add-adgroupmember -identity "MyADGROUP" -Members $_.SamAccountName}

 

Please remember that computer object should have at their end $, otherwise it will be considered as ad user account.

 

 

--------------

If you find this answer helpfull , Click on best response and give like

 

@farismalaeb 

 

Hi, this is what i get:

 

 

Method invocation failed because [Microsoft.ActiveDirectory.Management.ADComputer] does not contain a method named 'foreac
h'.
At line:1 char:1
+ (Get-ADComputer -Filter 'Name -like "desktop1*"' -properties displayname ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (foreach:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

 

 

Must there be a $ at desktop1$

@Surfer10 

When i put $ ehind the computername the error does not appear but it does also not adding the computer to the security group, just tested with "desktop1$"

When i do this:

Get-ADComputer -Filter 'Name -like "desktop1"' -properties displayname

 

The properties of the computer appears so thats okay.

 

 

@Surfer10 

@Surfer10 

it seems that you are using an old version of Powershell 

Try this

$AllPC=Get-ADComputer -Filter 'Name -like "Desktop*"' -properties displayname

foreach($SinglePC in $AllPC){
add-adgroupmember -identity "MyADGROUP" -Members $SinglePC.SamAccountName
}

@farismalaeb 

 

I think it is version on a WIndows Server 2012 en the version on my WIndows 10 client is 5 i believe, which comes standard with Windows 10.

 

On both Powershell versions the last is also not working, it gives no error but it does nothing, i hope you have some more suggetions:

 

$AllPC=Get-ADComputer -Filter 'Name -like "desktop1"' -properties displayname

foreach($SinglePC in $AllPC){
add-adgroupmember -identity "testgroup" -Members $SinglePC.SamAccountName
}

best response confirmed by Surfer10 (Occasional Contributor)
Solution

@Surfer10 

did you add the * after the desktop1

try this small change

$AllPC=Get-ADComputer -Filter 'SamAccountName -like "desktop*"'

Yes thank you now it works! @farismalaeb 

Hi, do you know whcih version i need for the other commands to work is that powershell version 7?

 

 

Now tht the script works is there also a way to exclude some names with DESKTOPNR?

 

I would like to run this command every week so that computers which are not a member of the testgroup will be added as member but there are 10 computers with the name dekstop111, desktop112,desktop250 and so 10 total.

 

Is it possible to exclude these, these 10 computers must be added to another group: prodgroup.

@Surfer10 

Its Powershell and everything is doable, but first, you need to think about it,

Logically what is the common thing between this computer object and build the filter based on that.

I would suggest updating a computer object attrib or custom attrib then exclude these object with these attrib from the query.