Powershell to export mailboxes

Posted on 2014-09-17
Last Modified: 2014-09-22
I am running the below command. I am poor in powershell.  Is it possible to loop the next command only after completing the first one?  
foreach ($i in (Get-Mailbox)) { New-MailboxExportRequest -Mailbox $i -FilePath "\\ExchPST\September\$($i.Alias).pst" -baditemlimit 50 -acceptlargedataloss }

Open in new window

Or at-least is t possible to give a time delay (e.g. sleeptimer, start-sleep) between each loop command?
Question by:☠MAS☠
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 40

Assisted Solution

Subsun earned 500 total points
ID: 40327439
foreach ($i in (Get-Mailbox)) { 

New-MailboxExportRequest -Mailbox $i -FilePath "\\ExchPST\September\$($i.Alias).pst" -baditemlimit 50 -acceptlargedataloss 

while ((Get-MailboxExportRequest -Identity $i | ? {$_.Status -eq “Queued” -or $_.Status -eq “InProgress”}))  { sleep 60 }

Open in new window

Above command will wait until the mailbox export request get completed. If you just want to insert a 60 sec delay after once user export, then use the following code..

foreach ($i in (Get-Mailbox)) { New-MailboxExportRequest -Mailbox $i -FilePath "\\ExchPST\September\$($i.Alias).pst" -baditemlimit 50 -acceptlargedataloss;Start-Sleep 60}

Open in new window

LVL 27

Author Comment

ID: 40327454
This is the error on the first command, Is it a single line command ? I tried both
A positional parameter cannot be found that accepts argument 'while'.
    + CategoryInfo          : InvalidArgument: (:) [New-MailboxExportRequest], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,New-MailboxExportRequest

Second one is working
LVL 40

Assisted Solution

Subsun earned 500 total points
ID: 40327457
First code should be used as it is, don't combine the commands and make in in single line.. If you want to make it in single line then use ; (semicolon) to separate the commands.

foreach ($i in (Get-Mailbox)) { New-MailboxExportRequest -Mailbox $i -FilePath "\\ExchPST\September\$($i.Alias).pst" -baditemlimit 50 -acceptlargedataloss ;while ((Get-MailboxExportRequest -Identity $i | ? {$_.Status -eq “Queued” -or $_.Status -eq “InProgress”})) { sleep 60 } }

Open in new window

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

LVL 27

Author Comment

ID: 40327465
Name                                                                 Mailbox                                                                  Status
----                                                                         -------                                                                      ------
MailboxExport                         Scaria                                    Queued
Cannot process argument transformation on parameter 'Identity'. Cannot convert the "Joseph Scaria" value of type "
Microsoft.Exchange.Data.Directory.Management.Mailbox" to type "Microsoft.Exchange.Management.RecipientTasks.MailboxExpo
    + CategoryInfo          : InvalidData: (:) [Get-MailboxExportRequest], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-MailboxExportRequest

It created the first request and showing this error.
If I am not mistaken identity is supposed to be "Joseph scaria\mailboxexport"
That is why it is showing error
LVL 40

Expert Comment

ID: 40327471
I am unable to test it right now.. What if you try..
foreach ($i in (Get-Mailbox)) { New-MailboxExportRequest -Mailbox $i -FilePath "\\ExchPST\September\$($i.Alias).pst" -baditemlimit 50 -acceptlargedataloss ;while ((Get-MailboxExportRequest -Identity $i.alias | ? {$_.Status -eq “Queued” -or $_.Status -eq “InProgress”})) { sleep 60 } }

Open in new window

foreach ($i in (Get-Mailbox)) {$move = New-MailboxExportRequest -Mailbox $i -FilePath "\\ExchPST\September\$($i.Alias).pst" -baditemlimit 50 -acceptlargedataloss ;while ((Get-MailboxExportRequest -Identity $move.Mailbox | ? {$_.Status -eq “Queued” -or $_.Status -eq “InProgress”})) { sleep 60 } }

Open in new window

LVL 27

Accepted Solution

☠MAS☠ earned 0 total points
ID: 40327498
I managed to get it done by this command. Thanks a lot.
foreach ($i in (Get-Mailbox)) { New-MailboxExportRequest -Mailbox $i -FilePath "\\ExchPST\September\$($i.Alias).pst" -baditemlimit 50 -acceptlargedataloss ;while ((Get-MailboxExportRequest -mailbox $i | ? {$_.Status -eq “Queued” -or $_.Status -eq “InProgress”})) { sleep 60 } }

Open in new window

LVL 27

Author Closing Comment

ID: 40336167

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses
Course of the Month5 days, 23 hours left to enroll

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question