locked
Powershell to check if the email address policy is set to automatically update an object and another to query if an email address is found RRS feed

  • Question

  • Hello,

    Can anyone point me or let me know the Powershell to check if the email address policy is set to automatically update an object for all users and another one to query if an address exists on an object.  We have two domains - company1.com and company2.com set but some users have had the auto policy removed and have removed the company2.com email address.  If we find any that missing the auto update policy and email address missing we need to add back in.

    Many Thanks.

    Wednesday, March 23, 2016 10:00 AM

Answers

  • Export-Csv will do that.  Here's the command to do that:

    Get-Mailbox –Resultsize Unlimited | Sort Name | Select Name, Alias, OrganizationalUnit, EmailAddressPolicyEnabled, @{E={ "$($_.EmailAddresses)" };L='EmailAddresses'} | Export-Csv D:\Addresspolicy.csv -NoTypeInformation


    Will Martin ...
    -join ('77696c6c406d617274696e2d66616d696c6965732e6f7267' -split '(?<=\G.{2})' | ? { $_ } | % { [char][int]"0x$_" })

    • Marked as answer by WORB68 Wednesday, March 30, 2016 2:23 PM
    Wednesday, March 30, 2016 12:59 PM

All replies

  • First, you need the commands to find how the address policy is set and what addresses are on the mailbox - for this, the following should work:

    Get-Mailbox <mailbox identity> | Select Name, Alias, EmailAddressPolicyEnabled, EmailAddresses

    To run this for all your mailboxes, you get a listing of all your mailboxes and pull the information for each:

    Get-Mailbox -ResultSize Unlimited | Sort Name | Select Name, Alias, EmailAddressPolicyEnabled, EmailAddresses

    If you want the output to only display those mailboxes that are set improperly, run the following:

    Get-Mailbox -ResultSize Unlimited | ? { -not $_.EmailAddressPolicyEnabled } | Sort Name | Select Name, Alias, EmailAddressPolicyEnabled, EmailAddresses

    If you want this in a file so you can search through it and take your next steps, add the following to the end of either of the above commands:

    | Export-Csv EmailAddressesToCorrect.csv -NoTypeInformation

    This should get you what you need.


    Will Martin ...
    -join ('77696c6c406d617274696e2d66616d696c6965732e6f7267' -split '(?<=\G.{2})' | ? { $_ } | % { [char][int]"0x$_" })

    • Proposed as answer by Lynn-Li Thursday, March 24, 2016 2:38 AM
    Wednesday, March 23, 2016 1:14 PM
  • Hi,

    In addition to what Will Martin said, if you want to add back in, use Set-Mailbox cmdlet after Get-Mailbox command

    Get-Mailbox -ResultSize Unlimited | ? { -not $_.EmailAddressPolicyEnabled } | Set-Mailbox -EmailAddressPolicyEnabled $true

    Then update email address policy

    Update-EmailAddressPolicy "Policy name"

    Best Regards.


    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    Lynn-Li
    TechNet Community Support

    Thursday, March 24, 2016 2:44 AM
  • Thanks for this - very helpful.  Is it possible to:

    1.  When running 

    Get-Mailbox –Resultsize Unlimited | Sort Name | Select Name, Alias, EmailAddressPolicyEnabled, EmailAddress >D:\Addresspolicy.csv

    Is it possible to exclude an OU or run it against particular OU's.  We have an OU where we cant enable the Email Address policy - in fact doing so will cause chaos :-)  So it would be handy to run the report against particular OU's and if needed to update only against them as well.

    Many Thanks.

    Thursday, March 24, 2016 11:52 AM
  • You can run against specific OUs by adding the -OrganizationalUnit parameter to the Get-Mailbox command.  Or you can run it against your entire organization, but add the OrganizationalUnit field to your output, as below:

    Get-Mailbox –Resultsize Unlimited | Sort Name | Select Name, Alias, OrganizationalUnit, EmailAddressPolicyEnabled, @{E={ "$($_.EmailAddresses)" };L='EmailAddresses'} >D:\Addresspolicy.csv

    An additional note, once you find mailboxes that don't have your policy, you can run a script that enables it, using the Set-Mailbox command.

    And a final note, since you are returning the EmailAddresses field, and it is multi-item, you will want it output the way I did above so that all the addresses are listed, and you don't see just the data type (Microsoft.Exchange.Data.ProxyAddressCollection) in the output.


    Will Martin ...
    -join ('77696c6c406d617274696e2d66616d696c6965732e6f7267' -split '(?<=\G.{2})' | ? { $_ } | % { [char][int]"0x$_" })

    Friday, March 25, 2016 1:12 PM
  • Will - thanks.  This is exactly what I wanted.

    Quick query though is there a way to create the file with the Fields along the top so I can filter in Excel to make reading this a whole lot easier.  So:

    Name Alias OU EmailAddressPolicyEnabled EmailAddresses

    User user company.com True user@company.com

    Thanks.

    Wednesday, March 30, 2016 11:31 AM
  • Export-Csv will do that.  Here's the command to do that:

    Get-Mailbox –Resultsize Unlimited | Sort Name | Select Name, Alias, OrganizationalUnit, EmailAddressPolicyEnabled, @{E={ "$($_.EmailAddresses)" };L='EmailAddresses'} | Export-Csv D:\Addresspolicy.csv -NoTypeInformation


    Will Martin ...
    -join ('77696c6c406d617274696e2d66616d696c6965732e6f7267' -split '(?<=\G.{2})' | ? { $_ } | % { [char][int]"0x$_" })

    • Marked as answer by WORB68 Wednesday, March 30, 2016 2:23 PM
    Wednesday, March 30, 2016 12:59 PM
  • That is perfect - just what I needed.

    Many Thanks.

    Wednesday, March 30, 2016 2:23 PM