2

I need a script that will change all of my ad users email contact field information. The format will be the "SAMAccountName + @domain.com". I am a powershell noob and need a little help on the syntax. Any help would be appreciated.

user157877
  • 21
  • 1
  • 1
  • 2
  • 1
    if you are using Exchange, E-mail Address Policies are the way to go for this – August Feb 06 '13 at 17:50
  • Do you have available the [ActiveDirectory Powershell modules](http://technet.microsoft.com/en-us/library/ee617195.aspx)? Is your domain 2008R2, or do you have [Active Directory Management Gateway Service](http://www.microsoft.com/en-us/download/details.aspx?id=2852) installed? – jscott Feb 06 '13 at 18:20
  • I do not have active directory powershell modules installed yet and yes my domain is 2008 r2. – user157877 Feb 06 '13 at 19:21

1 Answers1

5

Here is what I would do:

Install the Active Directory Powershell Module

Script it like this:

Import-Module ActiveDirectory
$users = Get-ADUser -Filter *
foreach ($user in $users)
{
    $email = $user.samaccountname + '@domain.com'
    Set-ADUser -Identity $user.samaccountname -EmailAddress $email
}
Mathias R. Jessen
  • 25,161
  • 4
  • 63
  • 95
  • $users | Foreach-Object { $email = $_.samaccountname ... would be the better choice here. Especially when processing a large amount of data. Read here for more information: https://blogs.technet.microsoft.com/heyscriptingguy/2014/07/08/getting-to-know-foreach-and-foreach-object/ – Tom Mar 16 '16 at 13:09
  • 1
    @Tom Unless you do something like `-Properties *`, it's not going to be that much of a difference, but fair point – Mathias R. Jessen Mar 16 '16 at 15:10
  • I know - I'm a bit nitpicking. But I think everyone should know the difference between the two methods. – Tom Mar 17 '16 at 13:07