troubleshooting Question

Powershell - Foreach Statement Error

Avatar of Isaias Perez
Isaias PerezFlag for United States of America asked on
PowershellIT AdministrationScripting Languages
6 Comments1 Solution129 ViewsLast Modified:
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.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros