Hash table export in csv/xml

I have a simple hash table with keys and values, like this:

Name Value
-----------
Key1 Value1
Key2 Value2
Key3 Value3

And I just need to export in file only keys and values. How can I do that?

GeorgyT,
Welcome to the forum. :wave:t3:

In the vast majority of the cases you’re not the very first one with a given task. So it is recommended and surprisingly often successful to search online if there is already a solution out there. Have you tried this?

Regardless of that sinse your output does not seem to be a proper hashtable … the output of hastable in PowerShell looks like this:

Name                           Value 
----                           ----- 
Key2                           Value2
Key1                           Value1
Key3                           Value3

… it is even unclear what exactly you are looking for. Would you expect something like this as result?

Key2   Key1   Key3
----   ----   ----
Value2 Value1 Value3
1 Like

I tried, but PS documentation is truly bad (in compare with another languages).
I just want to save information from hash table in file. And yes, it must be looks like:

Name                           Value 
----                           ----- 
Key2                           Value2
Key1                           Value1
Key3                           Value3

What kind of file? Lots of guides/docs out there.

$HashTable = @{
    Key1 = 'Value1'
    Key2 = 'Value2'
    Key3 = 'Value3'
}

$HashTable.GetEnumerator() |
Select-Object -Property Key, Value |
Export-Csv -NoTypeInformation -Path .\test.csv

Quick example of how to get it into a CSV file with Key/Value headers. You can rename ‘column headers’ using calculated properties if you desire:

$HashTable.GetEnumerator() |
Select-Object -Property @{Name = 'Property'; Expression = { $_.Key } }, Value |
Export-Csv -NoTypeInformation -Path .\test.csv

Have you tried any other source of information?

You could have mentioned that in your initial post.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.