Tuesday, 27 September 2022

Export array (table) into csv-file in Powershell

Export array (table) into csv-file in Powershell

 First of all, we need to define the array:

1
$export_array = @();

Then, fill in the data you need:

1
2
3
4
$export_array += ,@(""); # just an empty string
$export_array += ,@($a1, $b1, $c1);
$export_array += ,@($a2, $b2, $c2);
$export_array += ,@($a3, $b3, $c3);

Compose csv file name and path to it:

1
2
3
$datetime = Get-Date -Format "yyyy.MM.dd_HH-mm-ss";
$file_name = "audit_result_" + $datetime + ".csv";
$file_path = "./" + $file_name;

Actual export of array into csv-file:

1
2
3
4
5
6
7
8
9
foreach($item1 in $export_array) 
  $csv_string = "";
  foreach($item in $item1)
  {
    $csv_string = $csv_string + $item + ";";
  }
  Add-Content $file_path $csv_string;
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.