Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export data to CSV in PowerShell?

foreach ($computer in $computerlist) {     if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))     {         foreach ($file in $REMOVE) {             Remove-Item "\\$computer\$DESTINATION\$file" -Recurse             Copy-Item E:\Code\powershell\shortcuts\* "\\$computer\$DESTINATION\"                     }     } else {         Write-Host "\\$computer\$DESTINATION\"     } } 

I want to export Write-Host "\$computer\$DESTINATION\" to the CSV files so I know which computers were offline when the script ran.

I am running this from a Windows 7 machine

like image 600
software is fun Avatar asked Jan 10 '14 14:01

software is fun


People also ask

How do I Export data from PowerShell to excel?

As of now, there is no built-in command like CSV (Export-CSV) to export output to the excel file but we can use the Out-File command to export data to excel or any other file format. Let's use Out-File to export the output of the Get-Processes command to an excel file.

How do I add data to a CSV file in PowerShell?

To simply append to a file in powershell,you can use add-content. So, to only add a new line to the file, try the following, where $YourNewDate and $YourDescription contain the desired values. This will just tag the new line to the end of the . csv, and will not work for creating new .

How do I Export a value in PowerShell?

Example 1: Export process properties to a CSV fileThe process objects are sent down the pipeline to the Select-Object cmdlet. Select-Object uses the Property parameter to select a subset of process object properties. The process objects are sent down the pipeline to the Export-Csv cmdlet.


1 Answers

This solution creates a psobject and adds each object to an array, it then creates the csv by piping the contents of the array through Export-CSV.

$results = @() foreach ($computer in $computerlist) {     if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))     {         foreach ($file in $REMOVE) {             Remove-Item "\\$computer\$DESTINATION\$file" -Recurse             Copy-Item E:\Code\powershell\shortcuts\* "\\$computer\$DESTINATION\"                     }     } else {          $details = @{                             Date             = get-date                               ComputerName     = $Computer                                  Destination      = $Destination          }                                    $results += New-Object PSObject -Property $details       } } $results | export-csv -Path c:\temp\so.csv -NoTypeInformation 

If you pipe a string object to a csv you will get its length written to the csv, this is because these are properties of the string, See here for more information.

This is why I create a new object first.

Try the following:

write-output "test" | convertto-csv -NoTypeInformation 

This will give you:

"Length" "4" 

If you use the Get-Member on Write-Output as follows:

write-output "test" | Get-Member -MemberType Property 

You will see that it has one property - 'length':

   TypeName: System.String  Name   MemberType Definition ----   ---------- ---------- Length Property   System.Int32 Length {get;} 

This is why Length will be written to the csv file.


Update: Appending a CSV Not the most efficient way if the file gets large...

$csvFileName = "c:\temp\so.csv" $results = @() if (Test-Path $csvFileName) {     $results += Import-Csv -Path $csvFileName } foreach ($computer in $computerlist) {     if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))     {         foreach ($file in $REMOVE) {             Remove-Item "\\$computer\$DESTINATION\$file" -Recurse             Copy-Item E:\Code\powershell\shortcuts\* "\\$computer\$DESTINATION\"                     }     } else {          $details = @{                             Date             = get-date                               ComputerName     = $Computer                                  Destination      = $Destination          }                                    $results += New-Object PSObject -Property $details       } } $results | export-csv -Path $csvFileName -NoTypeInformation 
like image 69
David Martin Avatar answered Oct 07 '22 19:10

David Martin