user3432918
user3432918

Reputation: 131

Need to remove value in emailattribute AD/powershell

Hi i need to set the emailattribute to null/nothing on users. I have created this:

Get-ADGroupMember -Identity "testgrupp" | Get-ADUser -Properties samaccountname | Foreach {
Set-ADUser -Identity $_ -EmailAddress ("")
}

But it fails, i think its the last part that's wrong?

Upvotes: 0

Views: 1919

Answers (2)

Raf
Raf

Reputation: 10107

Substitute ("") for $null:

Get-ADGroupMember -Identity "testgrupp" | 
Get-ADUser -Properties samaccountname | Foreach {
    Set-ADUser -Identity $_ -EmailAddress $null
}

Upvotes: 1

mjolinor
mjolinor

Reputation: 68273

A couple of ways to fix this:

Use the -ExpandProperty parameter of Select-Object so you only get the samaccountname strings:

Get-ADGroupMember -Identity "testgrupp" |
 Get-ADUser -Properties samaccountname | 
 Select -ExpandProperty samaccountname |
 Foreach {
          Set-ADUser -Identity $_ -EmailAddress ("")
        }

or reference the samaccountname property in your Set-ADUser:

Get-ADGroupMember -Identity "testgrupp" | 
Get-ADUser -Properties samaccountname | 
Foreach {
         Set-ADUser -Identity $_.samaccountname -EmailAddress ("")
        }

Upvotes: 1

Related Questions