I'm using this script below to count the number of folders that certain mailboxes have. I am currently running into a problem. If I want to check 10 mailboxes and suppose that 1 mailbox has no folder at all, Powershell skips it in the export. So I only have 9 rows in my csv file and I can never know which mailbox was skipped. How can I ensure that the email address is also exported during this script?
$users = gc "c:\temp\user-list.txt"
foreach ($user in $users) { (Get-mailboxfolderstatistics -identity $user).Count | fl | out-file -FilePath C:\emails.csv -append}
The user-list.txt file contains:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
The export file emails.csv looks like this:
- 50
- 36
- 35
- 35
- 36
- 36
- 36
- 49
- 36
CodePudding user response:
Instead of outputting the count alone, create an object that has both the mailbox name and count attached, then use Export-Csv
to create the CSV file:
$users |Select @{Name='Username';Expression={$_}},@{Name='FolderCount';Expression={@(Get-mailboxfolderstatistics -identity $_).Count}} |Export-Csv C:\emails.csv -NoTypeInformation