morrisstu
morrisstu

Reputation: 99

Using multiple CSV columns to import data for mailbox export

I'm trying to export a few hundred mailboxes to a .PST file. I have a CSV file with two columns which are

  1. E-Mail Address, and
  2. User Logon Name

I need the login name to specify which mailbox to export.

Ideally I'd like it to be able to cycle through unique login name and export the mailbox with the format emailaddress.pst (the below command works while typed in static) but I can't seem to be able to cycle through the login names correctly.

I get an error about an invalid character length however if I simply try to echo it to the console it gives me each name as a separate entry as I would like e.g

AUser
BUser
CUser

Any advice?

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;

$File = Import-Csv "sourcefile.csv"
$EmailAddr = @($File | select -ExpandProperty "E-Mail Address")
$index = 0
$ADLogonName = @($File | select -ExpandProperty "User Logon name")

foreach ($addr in $EmailAddr) {
    New-MailboxExportRequest -Mailbox $ADLogonName[$index] -FilePath  \\networkshare\$addr.pst
    $index++
}

Upvotes: 1

Views: 567

Answers (1)

Shawn Esterman
Shawn Esterman

Reputation: 2342

I think you'd be able to solve your problem by avoiding an index and just doing a foreach. That way you ensure that each $Entry is the matching User Logon name and E-Mail Address.

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
$File = Import-Csv "sourcefile.csv"
foreach ( $Entry in $File )
{
    New-MailboxExportRequest -Mailbox $Entry.'User Logon name' -FilePath "\\networkshare\$($Entry.'E-Mail Address').pst"
}

Upvotes: 5

Related Questions