• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 59
  • Last Modified:

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.
0
grahamco
Asked:
grahamco
  • 2
  • 2
2 Solutions
 
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
 
QlemoBatchelor, 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
 
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
 
QlemoBatchelor, 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now