I am trying to archive mailboxes (which are no longer needed) from an on-premises Exchange 2010 environment to PST files in a directory on a remote server share. My problem is that I am unable to figure out the proper syntax in PowerShell to somewhat automate this process. The closest that I’ve come to it working is the single quotes being replaced by whitespaces in my PowerShell command.
So, I try to run the following (as an example) from PS on one of our Exchange 2010 servers:
Get-Mailbox test.user | Where {$_.OrganizationalUnit -eq "domain.com/Users/TEMP"} | ForEach {New-MailboxExportRequest -Mailbox $_.Alias -FilePath '\\server\share\PSTdir\'$_.Alias'.pst'}
The test.user is only being used in the example as a test mailbox until I can work out the PowerShell syntax needed to make this work. Once the syntax works, I’ll remove the individual test.user mailbox and let PS send all mailboxes through the pipe.
When I am using the example, I can check the status of the move request by using Get-MailboxExportRequest. When I check the status, I see that the file path does not contain the properly formatted file path information. From the example, this is what part of the command is being formatted as in PS:
New-MailboxExportRequest -Mailbox test.user -FilePath \\server\share\PSTdir\ test.user .pst
Note there is now a space in “PSTdir\ test.user” and also a space before the .pst in “test.user .pst”. I’ve tried using single and double quotes, I’ve changed the location of the quotes, used escape character, etc., but I’ve not been able to figure out what I am missing. It seems that the single quote in the example is replaced with a whitespace character (which I need to be removed). The command should look something like this (with no spaces in the file path):
New-MailboxExportRequest -Mailbox test.user -FilePath \\server\share\PSTdir\test.user.pst
Q: How can I pass the variable $_.Alias through the PS pipe so that the name in the file path does not contain any spaces.