Export Mailboxes as PST by Last Logon Date

Travis Laira 161 Reputation points
2021-04-06T09:57:14.983+00:00

Dear Team,

I have an exchange 2016 environment, most of my mailboxes has been migrated to office 365. Now only active mailboxes has been migrated to office 365, i want to archive the rest of the mailboxes. I want to export them as pst and store them in a shared storage.

  1. I need a script to export the list of mailboxes as pst files, but i need the list from their last logon date so that i can export only the ones that has not migrated to office 365.
  2. Now i have moved these mailboxes to a 4 specific databases, if Number 1. could not be achieved, can I export all mailboxes from a specific database as pst?

so far i have tried this;
$Export = Get-Mailbox
$Export|%{$|New-MailboxExportRequest -FilePath “\filepath\$($.name).pst”}

The script is perfect, it can export mailboxes as pst including their name but the only issue is it exports all mailboxes.

Grateful for your assistance.

Thank you.

Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,349 questions
0 comments No comments
{count} votes

Accepted answer
  1. Travis Laira 161 Reputation points
    2021-05-28T05:07:36.55+00:00

    Dear All,

    Apologies for late feedback. Both commands provided work to a certain extent however my exchange didnt export all of the mailbox, it remained in a queued state.

    The following command below worked like a charm;

    1. Export the list of mailbox as csv / txt - similar to this: Get-Mailbox -ResultSize Unlimited|Get-MailboxStatistics|Select displayname,lastlogontime| sort lastlogontime -Descending| Export-CSV c:\temp\data.csv -NoTypeInformation
    2. Remove the other tabs leaving the mailbox names for example;
      1. User1@keyman .com
      2. User2@keyman .com
      3. User3@keyman .com
        etc....
    3. Save the csv on the exchange server in any location, for example data.csv
    4. Open a notepad on the exchange server and save the following command as .ps1

    $Mailboxes=get-content C:\temp\data.CSV
    foreach($mailbox in $mailboxes)
    {
    New-MailboxExportRequest -Mailbox $mailbox -FilePath "\Network path\$mailbox.pst"}

    1. Run the command using Exchange Management Shell

    Hope this is helpful, thanks for all your help, all answers has been really helpful.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Troy Werelius 76 Reputation points
    2021-04-06T14:08:09.44+00:00

    Here you go and you need to replace the sections w=ith angle brackets <> to match your information.

    ForEach ($Mailbox in (Get-Mailbox -Database <Database ID>)) {New-MailboxExportRequest -Mailbox $Mailbox -FilePath “\<server FQDN>\<shared folder name>\$($Mailbox.Alias).pst” }

    Search, Recover, & Extract Mailboxes, Folders, & Email Items from Offline Exchange Mailbox and Public Folder EDB's and Live Exchange Servers or Import/Migrate direct from Offline EDB to Any Production Exchange Server, even cross version i.e. 2003 --> 2007 --> 2010 --> 2013 --> 2016 --> 2019 --> Exchange Online with Lucid8's DigiScope

    0 comments No comments

  2. Eric Yin-MSFT 4,386 Reputation points
    2021-04-07T02:23:05.02+00:00

    Hi,
    I'm answering your question 1, here are two methods:
    1 Use the following command to export all mailboxes' last logon time to csv

    Get-Mailbox -ResultSize Unlimited|Get-MailboxStatistics|Select displayname,lastlogontime| sort lastlogontime -Descending| Export-CSV c:\temp\Mailbox.csv -NoTypeInformation  
    

    Then you sort them in Excel, delete those mailboxes that you have moved and save the csv file to mailbox1.csv, then export the rest with command:

    ForEach ($Mailbox in (Import-Csv c:\temp\mailbox1.csv)) {New-MailboxExportRequest -Mailbox $Mailbox.displayname -FilePath “\\<server FQDN>\<shared folder name>\$($Mailbox.Alias).pst” }  
    

    2 Maybe you are not familiar with Excel, you can run the following command to return all mailboxes that haven't logon for 30 days (or any days you want):

    Get-MailBox -ResultSize Unlimited | Get-MailboxStatistics |? {$_.Lastlogontime -lt (get-date).AddDays(-30)}| sort lastlogontime -Descending  
    

    (Some other mailbox types should be listed, not sure if you need them) If those are mailboxes that you want to move, use the following to export them:

    $Export = Get-MailBox -ResultSize Unlimited | Get-MailboxStatistics |? {$_.Lastlogontime -lt (get-date).AddDays(-30)}| sort lastlogontime -Descending  
    $Export|%{$|New-MailboxExportRequest -FilePath “\\filepath\$($.name).pst”}  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.