Forum Discussion

Maher Ramadan's avatar
Maher Ramadan
Copper Contributor
Jan 22, 2022
Solved

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, 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}

  • 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

4 Replies

  • Maher Ramadan's avatar
    Maher Ramadan
    Copper 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's avatar
      VasilMichev
      MVP
      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 Ramadan's avatar
        Maher Ramadan
        Copper Contributor
        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

Resources