Blanking out emial field in AD

Welcome Forums General PowerShell Q&A Blanking out emial field in AD

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
2 months, 1 week ago.

  • Author
    Posts
  • #164803

    Participant
    Topics: 6
    Replies: 8
    Points: 17
    Rank: Member

    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?

     

     

  • #164962

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,438
    Helping Hand
    Rank: Community Hero

    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}

  • #165013

    Participant
    Topics: 6
    Replies: 8
    Points: 17
    Rank: Member

    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.

  • #165022

    Participant
    Topics: 1
    Replies: 1528
    Points: 2,581
    Helping Hand
    Rank: Community Hero

    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!

  • #165073

    Participant
    Topics: 6
    Replies: 8
    Points: 17
    Rank: Member

    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
    }

     

You must be logged in to reply to this topic.