SOLVED

only | Export-csv is having an issue exporting the data, the rest of the PS works fine

Brass Contributor

i run the below against a CSV file and works fine on exchange online PS,

foreach($m in $mailboxes) {Get-MailboxStatistics $m.Identity.ToString() |select Identity, displayname, LastLogonTime, itemcount, totalitemsize; Start-Sleep -Milliseconds 500}

 

however when i add the Export-csv switch it only gets one user info exported, the last user in the list

i tried altering the line in many ways something is wrong while exporting the results only , the results are retrieved successfully on-screen when the command is fired without the Export parameter

 

foreach($m in $mailboxes) {Get-MailboxStatistics $m.Identity.ToString() |select Identity, displayname, LastLogonTime, itemcount, totalitemsize; Start-Sleep -Milliseconds 500 -Export-csv d:\123.csv}

4 Replies
something needs to be done on the command to make sure all data are exported not only the last value, also i have noticed that i only get that last user in the list when i remove the start-sleep switch, if i don't i get empty CSV file no exported data at all, yet without the export, i can get the data on screen
Since you are running Export-CSV inside the foreach loop, the file gets overwritten at each iteration, that's expected. Either move the Export-CSV cmdlet outside of the loop, or use the -Append switch to "add" to the file, instead of overwriting it.
best response confirmed by Maher Ramadan (Brass Contributor)
Solution
The append didn’t work for me guess it is used to add more than one get- to the same file

However the other approach extracting the results then exporting them worked just fine, thank you


$Results = foreach($m in $mailboxes) {Get-MailboxStatistics $m.Identity.ToString() |select Identity, displayname, LastLogonTime, itemcount, totalitemsize; Start-Sleep -Milliseconds 500}
$Results | Export-CSV -Path " D:\123.csv" -NoTypeInformation
this way the results are exported then written to the file, hope it helps someone as poor as i am in scripting :)
1 best response

Accepted Solutions
best response confirmed by Maher Ramadan (Brass Contributor)
Solution
The append didn’t work for me guess it is used to add more than one get- to the same file

However the other approach extracting the results then exporting them worked just fine, thank you


$Results = foreach($m in $mailboxes) {Get-MailboxStatistics $m.Identity.ToString() |select Identity, displayname, LastLogonTime, itemcount, totalitemsize; Start-Sleep -Milliseconds 500}
$Results | Export-CSV -Path " D:\123.csv" -NoTypeInformation

View solution in original post