Export Object to CSV

When I run the following code with write-output $obj at the end It prints all of the computers on the screen but when I try to export to csv I only get (one computer) the last computer information in the csv file.

$Computers = get-content C:\POSH\test.txt
foreach ($comp in $Computers) {
  $CS = Get-WmiObject -Class Win32_computersystem -ComputerName $comp
  $Encl = Get-WmiObject -Class Win32_Systemenclosure -ComputerName $comp

  $props = @{CompName=$cs.name

    $obj = New-Object -TypeName PSObject -property $props
    Write-Output $obj
 #  $obj | export-csv c:\posh\test.csv -Notype (I only get one computer when I replace write-output with this)

You need to use the -append switch. That will add the output to the end of the file.

thanks! :slight_smile: that worked great. so simple

Or put the export last outside the loop:

} | export-csv c:\posh\test.csv -Notype