SOLVED

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

%3CLINGO-SUB%20id%3D%22lingo-sub-3069294%22%20slang%3D%22en-US%22%3Eonly%20%7C%20Export-csv%20is%20having%20an%20issue%20exporting%20the%20data%2C%20the%20rest%20of%20the%20PS%20works%20fine%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3069294%22%20slang%3D%22en-US%22%3E%3CP%3Ei%20run%20the%20below%20against%20a%20CSV%20file%20and%20works%20fine%20on%20exchange%20online%20PS%2C%3C%2FP%3E%3CP%3Eforeach(%24m%20in%20%24mailboxes)%20%7BGet-MailboxStatistics%20%24m.Identity.ToString()%20%7Cselect%20Identity%2C%20displayname%2C%20LastLogonTime%2C%20itemcount%2C%20totalitemsize%3B%20Start-Sleep%20-Milliseconds%20500%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ehowever%20when%20i%20add%20the%20Export-csv%20switch%20it%20only%20gets%20one%20user%20info%20exported%2C%20the%20last%20user%20in%20the%20list%3C%2FP%3E%3CP%3Ei%20tried%20altering%20the%20line%20in%20many%20ways%20something%20is%20wrong%20while%20exporting%20the%20results%20only%20%2C%20the%20results%20are%20retrieved%20successfully%20on-screen%20when%20the%20command%20is%20fired%20without%20the%20Export%20parameter%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eforeach(%24m%20in%20%24mailboxes)%20%7BGet-MailboxStatistics%20%24m.Identity.ToString()%20%7Cselect%20Identity%2C%20displayname%2C%20LastLogonTime%2C%20itemcount%2C%20totalitemsize%3B%20Start-Sleep%20-Milliseconds%20500%20-Export-csv%20d%3A%5C123.csv%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3069294%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOMS%20Operations%20Management%20Suite%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional 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 (Occasional 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 :)