Forum Discussion
Maher Ramadan
Jan 22, 2022Brass Contributor
only | Export-csv is having an issue exporting the data, the rest of the PS works fine
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, itemcou...
- Jan 23, 2022The 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
Maher Ramadan
Jan 22, 2022Brass Contributor
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
VasilMichev
Jan 23, 2022MVP
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.
- Maher RamadanJan 23, 2022Brass ContributorThe 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- Maher RamadanJan 23, 2022Brass Contributorthis way the results are exported then written to the file, hope it helps someone as poor as i am in scripting 🙂