0

I'm in need of a Powershell script that will edit all users email attribute within a specific OU in Active Directory.

The code below searches for the correct OU and edits the users email attributes within, however the users email attributes are being replaced with @domain.com instead of [email protected].

Is my syntax wrong: $($_.samaccountname)?

Get-ADUser -Filter * -SearchBase "OU=Test,OU=People,DC=ad,DC=domain,DC=com" | Set-ADUser -email "$($_.samaccountname)@domain.com"

I appreciate the help.

2 Answers 2

2

The problem is not your syntax, but that the variable $_ doesn't exist in this context.

To use it you need to be inside a foreach loop.

Get-ADUser -Filter * -SearchBase "OU=Test,OU=People,DC=ad,DC=domain,DC=com" |
    Foreach-Object { Set-ADUser -email "$($_.samaccountname)@domain.com" }
0
Set-ADUser -email "$($_.samaccountname)@domain.com"

It's quite simpler than that. Just use

Set-ADUser -email ($_.samaccountname + "@domain.com")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .