Avatar of Isaias Perez
Isaias Perez
Flag for United States of America

asked on 

Powershell - Foreach Statement Error

I am encountering a weird error and its boggling my mind. Can you powershell experts please show me what i am doing wrong. I am trying to put together a foreach statement that will readd AD groups to an AD User object/Member. So whats odd is that if i used the below PS code it works manually but does not work with variables and the foreach code below. It errors our stating cant find identity. But why when i manually input the same identity that is looping through it errors our. So for example if i type:

Add-ADGroupMember - OPSC -Members 1078477

Open in new window


It works just fine.. I verify and the AD Group is added to member 1078477

But when i insert this with variables and a foreach statement it states it cant find the identity? I verify that whats looping through is correct by executing the variable by itself.

$adgroups = Get-Content -Path C:\Scripts\adgroups.txt
foreach ($group in $adgroups) { Add-ADGroupMember - $group -Members 1078477}

Open in new window


Ive been playing with foreach statements all day trying to finetune my PS skills and understand the difference between foreach and Foreach-Object and Get-Content and Import-CSV for my own knowledge.

foreach-statement-erro.pngadgroups.png
PowershellIT AdministrationScripting Languages

Avatar of undefined
Last Comment
Isaias Perez

8/22/2022 - Mon