SOLVED

Exporting Results To .CSV File

%3CLINGO-SUB%20id%3D%22lingo-sub-1241719%22%20slang%3D%22en-US%22%3EExporting%20Results%20To%20.CSV%20File%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1241719%22%20slang%3D%22en-US%22%3E%3CP%3EForgive%20me%20if%20this%20is%20obvious%20but%20I%20am%20still%20fairly%20new%20to%20Powershell%20and%20have%20tried%20alot%20but%20not%20found%20a%20reason%20for%20why%20this%20is%20not%20working.%20I%20have%20the%20below%20script%20that%20works%20find%20but%20when%20I%20try%20to%20pipe%20an%20%22Export-csv%22%20the%20result%20is%20a%20blank%20excel%20sheet.%20So%20my%20question%20is%20how%20can%20I%20use%20the%20script%20and%20export%20the%20results%20to%20.CSV%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%24users%20%3D%20get-content%20%22c%3A%5Ctemp%5Cislands%20with%20notify.txt%22%0Aforeach%20(%24user%20in%20%24users)%7B%0AGet-CSonlineuser%20-identity%20%24user%20%7C%20select-object%20userprincipalname%2CTeamsUpgradePolicy%20%7C%20export-csv%20c%3A%5Ctemp%5CTeamsupgradepolicy.csv%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1241719%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExport%20csv%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1242354%22%20slang%3D%22en-US%22%3ERe%3A%20Exporting%20Results%20To%20.CSV%20File%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1242354%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20way%20you've%20done%20it%2C%20the%20CSV%20file%20is%20overwritten%20after%20each%20iteration.%20You%20either%20need%20to%20put%20the%20Export-CSV%20cmdlet%20outside%20of%20the%20loop%2C%20or%20use%20the%20-Append%20switch%20to%20make%20it%20add%20to%20the%20file%2C%20not%20overwrite%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAlso%2C%20when%20importing%20CSV%20files%2C%20PowerShell%20will%20treat%20items%20as%20objects.%20%24user%20in%20your%20case%20will%20represent%20the%20entire%20object%20(think%20of%20it%20the%20entire%20row%20in%20the%20CSV%20file)%2C%20but%20not%20all%20cmdlets%20will%20accept%20such%20input.%20Instead%2C%20you%20should%20use%20something%20like%20%24user.Name%20or%20%24user.UserPrincipalName%20-%20those%20properties%20represent%20the%20column%20names%20in%20your%20CSV%20file.%3C%2FP%3E%3C%2FLINGO-BODY%3E
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 (Contributor)
Solution

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.