Forum Discussion
add computers to security group automatically
- Jan 21, 2021
did you add the * after the desktop1
try this small change
$AllPC=Get-ADComputer -Filter 'SamAccountName -like "desktop*"'
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
- Surfer10Jan 21, 2021Copper Contributor
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 : MethodNotFoundMust there be a $ at desktop1$