Exporting Results To .CSV File

Frequent Contributor

Forgive me if this is obvious but I am still fairly new to Powershell and have tried alot but not found a reason for why this is not working. I have the below script that works find but when I try to pipe an "Export-csv" the result is a blank excel sheet. So my question is how can I use the script and export the results to .CSV?


$users = get-content "c:\temp\islands with notify.txt"
foreach ($user in $users){
Get-CSonlineuser -identity $user | select-object userprincipalname,TeamsUpgradePolicy | export-csv c:\temp\Teamsupgradepolicy.csv


Thanks in advance!


1 Reply
best response confirmed by charlie4872 (Frequent Contributor)

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.