Blanking out emial field in AD

I need to delete the email address field from about 200 users. I was able to generate a csv file and run this code. Think of these users like vendors that you setup an accounts for to do some kind of remote work, most of these are for SSL VPN for us. We want to have an email address on record for them but not in the email address field.

Import-CSV C:\temp\users_with_email_addresses.csv | ForEach-Object {
$samaccountname = $_.samaccountname
Get-ADUser -Identity $samaccountname -Properties emailaddress}

I am running this under my domain admin account. This shows me all the accounts and the email address associated with them.

I thought it would be as easy as sending that info across the pipe line to set-aduser. I am using this code.

Import-CSV C:\temp\users_with_email_addresses.csv | ForEach-Object {
$samaccountname = $_.samaccountname
$emailaddress = $_.emailaddress
Get-ADUser -Identity $samaccountname | Set-ADUser -Remove $emailaddress
}

I have used multiple variations of the set-aduser. I have used the -replace parameter. I have tried $null, $_.null set directly to the variable. I have tried leaving the emailaddress cell in my CSV file blank.

In the current iteration above I get the error message “set-aduser : cannot bind parameter ‘remove’. Cannot convert the “user@emailaddres.com” value of type “System.string” to type “system.collection.hashtable”.”

How do I use set-aduser to blank out the email field in AD?

 

 

You would pass a hashtable to the Remove parameter like below.

 Set-ADUser -Remove @{mail="something@example.com"} 

you will get mail from $_ , @{mail=$_.mail}

Thank you for the reply, but not what I am looking for. I need the email field for these users to be completely blank like it was never filled in at creation. I am trying to pass something through that will delete what is there now and leave it blank or null.

Please read the complete help for Set-ADUser including the examples to learn how to use it. You should pay special attention to the parameter -Clear!

Good news it works. Thanks for the help. Here is the resulting code.

Import-CSV C:\temp\users_with_email_addresses.csv | ForEach-Object {
$samaccountname = $_.samaccountname
Get-ADUser -Identity $samaccountname | Set-ADUser -clear mail
}