Powershell Hashtable Export to CSV -
i trying create csv export contains rows in data spreadsheet ids search spreadsheet show in.
i have managed create searching element through powershell having trouble exporting data csv format.
below example tables, actual data has 8 values (including id column), first 3 guaranteed filled.
data spreadsheet
+------+---------+---------+---------+---------------------+ | id | value 1 | value 2 | value 3 | value 4 | +------+---------+---------+---------+---------------------+ | 1234 | london | serial1 | hp | laptop user | | 2345 | moscow | serial7 | | hr application | | 1234 | london | serial9 | | finance application | | 3456 | madrid | serial4 | hp | laptop user | +------+---------+---------+---------+---------------------+
search spreadsheet
+------+ | id | +------+ | 1234 | | 2345 | +------+
desired result
+------+---------+---------+---------+---------------------+ | id | value 1 | value 2 | value 3 | value 4 | +------+---------+---------+---------+---------------------+ | 1234 | london | serial1 | hp | laptop user | | 2345 | moscow | serial7 | | hr application | | 1234 | london | serial9 | | finance application | +------+---------+---------+---------+---------------------+
below current code have attempts export csv removed.
$finalvalues = @{} $users = import-csv "search.csv" $data = import-csv "data.csv" | group-object -property id -ashashtable foreach ($user in $users) { if ($data.contains($user.id)) { #write-output $data[$user.id].id $finalvalues.add($data[$user.id].id, $data[$user.id]) } }
the following 2 commands (ran after rest of script has executed) have below output.
$finalvalues.values
id : 1234 value 1 : london value 2 : serial 1 value 3 : hp value 4 : value 5 : value 6 : value 7 : laptop user id : 2345 value 1 : moscow value 2 : serial7 value 3 : value 4 : value 5 : value 6 : value 7 : hr application id : 1234 value 1 : london value 2 : serial9 value 3 : value 4 : value 5 : value 6 : value 7 : finance application
$finalvalues
{1234, 1234} {@{id=1234; value 1=london; value 2=serial1; value 3=hp; value 4=; value 5=; value 6=; value 7=laptop user}, @{id=1234; value 1=london; value 2=serial 9... ; value 7 =finance application}} 2345 {@{id=2345; value 1=moscow; value 2=serial7; value 3=; value 4=; value 5=; value 6=; value 7=hr application}}
when exporting csv following command following result: $finalvalues | export-csv -path test.csv -notypeinformation
+------------+-------------+----------------+--------------------------------------------+----------------------------------------------+---------------+-------+ | isreadonly | isfixedsize | issynchronized | keys | values | syncroot | count | +------------+-------------+----------------+--------------------------------------------+----------------------------------------------+---------------+-------+ | false | false | false | system.collections.hashtable+keycollection | system.collections.hashtable+valuecollection | system.object | 14 | +------------+-------------+----------------+--------------------------------------------+----------------------------------------------+---------------+-------+
when exporting csv following command following result:
$finalvalues.values | export-csv -path test.csv -notypeinformation
+-------+------------+-------------+---------------+----------------+ | count | isreadonly | isfixedsize | syncroot | issynchronized | +-------+------------+-------------+---------------+----------------+ | 1 | false | false | system.object | false | | 1 | false | false | system.object | false | | 3 | false | false | system.object | false | | 1 | false | false | system.object | false | | 1 | false | false | system.object | false | | 1 | false | false | system.object | false | | 1 | false | false | system.object | false | | 1 | false | false | system.object | false | | 2 | false | false | system.object | false | | 2 | false | false | system.object | false | | 1 | false | false | system.object | false | | 2 | false | false | system.object | false | | 1 | false | false | system.object | false | | 1 | false | false | system.object | false | +-------+------------+-------------+---------------+----------------+
this simplified using where-object
filter , exporting export-csv
.
$search = import-csv "search.csv" import-csv "data.csv" | where-object {$search.id -contains $_.id} | export-csv c:\exampleexportpath.csv -notypeinformation
Comments
Post a Comment