Forum Discussion
Exporting Results To .CSV File
- Mar 20, 2020
The way you've done it, the CSV file is overwritten after each iteration. You either need to put the Export-CSV cmdlet outside of the loop, or use the -Append switch to make it add to the file, not overwrite it.
Also, when importing CSV files, PowerShell will treat items as objects. $user in your case will represent the entire object (think of it the entire row in the CSV file), but not all cmdlets will accept such input. Instead, you should use something like $user.Name or $user.UserPrincipalName - those properties represent the column names in your CSV file.
The way you've done it, the CSV file is overwritten after each iteration. You either need to put the Export-CSV cmdlet outside of the loop, or use the -Append switch to make it add to the file, not overwrite it.
Also, when importing CSV files, PowerShell will treat items as objects. $user in your case will represent the entire object (think of it the entire row in the CSV file), but not all cmdlets will accept such input. Instead, you should use something like $user.Name or $user.UserPrincipalName - those properties represent the column names in your CSV file.