Author Posts

July 19, 2017 at 7:31 pm

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
             Manufacturer=$cs.manufacturer
             Model=$cs.model
             Serial=$encl.serialnumber}

    $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)
}

July 19, 2017 at 8:02 pm

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

July 19, 2017 at 8:17 pm

thanks! 🙂 that worked great. so simple

July 21, 2017 at 7:01 pm

Or put the export last outside the loop:

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