Exchange PowerShell scripting

I want to run this exchange 2010 cmdlet: Get-Mailbox -Identity "mailboxnamehere" | Get-MailboxFolderStatistics | Select Identity,name,folderType,ItemsInFolder | Export-Csv -Path "C:\MIS\O365Migrations\"mailboxnamehere"_Pre-O365migration.csv". I require assistance with making the mailbox identity be a user inputted value and require assistance with the csv filename so that it includes the mailbox name.
grahamcoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeTwo SoftwareSoftware DeveloperCommented:
Hi grahamco,
I'm not sure if I understand what you mean by mailbox name being a user inputted value, but from the file name I assume you might want to run the script for all users. If that is the case, please try the following syntax:
$users=get-mailbox;
foreach($user in $users) { Get-MailboxFolderStatistics $user | Select Identity,name,folderType,
ItemsInFolder | Export-Csv -Path "C:\mig\$user.samaccountname_Pre-O365migration.csv"}

Open in new window

1
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The above contains an error in generating the CSV file name:
$users = get-mailbox 
foreach ($user in $users) {
  Get-MailboxFolderStatistics $user |
    Select Identity,name,folderType, ItemsInFolder |
    Export-Csv -Path "C:\mig\$($user.samaccountname)_Pre-O365migration.csv"}

Open in new window

but I would rewrite it
$users = $null
Get-MailBox $users |
% { 
  $user = $_.SamAccountName
  Get-MailboxFolderStatistics $_ |
    Select Identity, Name, FolderType, ItemsInFolder |
    Export-Csv -Path "C:\mig\${user}_Pre-O365migration.csv"
}

Open in new window

If you want to have a manual input of the user to export, just replace the first line with
$users = read-host 'Please provide a user name'

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
grahamcoAuthor Commented:
The first user input is the mailbox identity which will also be part of the .csv filename which the results are exported into. This script will be ran when migrating an on-premise mailbox to O365. We are migrating users in groups based on the delegate permissions needed so i text file with all our users wouldn't necessarily work.
0
grahamcoAuthor Commented:
do i need the "foreach ($user in $users)" since i won't be using the textfile containing all users? if not can you confirm this is how the script would look:

$user = read-host 'Please provide a user name'
{Get-MailboxFolderStatistics $user | Select Identity,name,folderType, ItemsInFolder | Export-Csv -Path "C:\mi$($user.samaccountname)_Pre-O365migration.csv"}
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
No. If you enter the name manually, it is a string, and no AD object, so there is no property samAccountName. That is one reason why I used a foreach loop in my second code snippet.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.