200mg
200mg

Reputation: 531

Finding AD Distro Group samaccountname with email address

I need to move a few hundred AD distro groups to a new OU. I was given their email address only, and wanted to make a script to move them based on samaccountname. I am wondering why the below does not return anything, if I do the groups one-off filtering for email address it works, but foreach returns nothing.

The "groups.txt" listed below is just a list of email addresses.

gc groups.txt | % {
  Get-ADGroup -Filter {mail -eq "$_"}| Select-Object -ExpandProperty SamAccountName
}

Upvotes: 2

Views: 3685

Answers (2)

bentek
bentek

Reputation: 245

Remove the quotes you have around $_.

gc groups.txt | % {
  Get-ADGroup -Filter {mail -eq $_}| Select-Object -ExpandProperty SamAccountName
}

In your posted filter script block the variable is quoted. Since it is a script block, PowerShell doesn't do any processing first and the ActiveDirectory module expects a variable not surrounded in quotes. That would look for Mail that is literally "$_" and not the email address value of the variable.

Upvotes: 2

Nate
Nate

Reputation: 862

Take the "" off of $_

gc groups.txt | %{ Get-ADGroup -Filter {mail -eq "$_"} | select -expandproperty samaccountname}

Upvotes: 2

Related Questions