-1

I am trying to convert a hashtable back to key-value pair in an efficient way. Currently I am using this:

$kv = ""
$hash.GetEnumerator() | ForEach {
  $kv += "$($_.Name)=$($_.Value)"
}

Isn't there any way to directly convert hash table to key value pairs, or I mean string data. There is ConvertFrom-StringData to convert key value pairs to hash table. Isn't there any way to do the opposite, convert hash tables to key value pairs directly?

E.G(Key-Value pair)

a=1
b=2
c=3
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • A hash table _is_ key-value store. Are you trying to create an array of strings? A JSON? Something else? Please edit the question and show the desired output. – vonPryz Mar 26 '20 at 08:45
  • Does this answer your question? [Does PowerShell support HashTable Serialization?](https://stackoverflow.com/questions/60621582/does-powershell-support-hashtable-serialization) – iRon Mar 26 '20 at 09:15
  • @vonpryz I am trying to create key value pairs look at the question. – Wasif Mar 26 '20 at 09:53
  • There is no built-in way, no (if by "efficient" you mean "a one-liner"; runtime efficiency should not be concern here). Check the linked question for various alternatives. Writing a function for it is not completely trivial due to the need to keep escaping in mind. – Jeroen Mostert Mar 26 '20 at 10:05
  • @iRon nope that does not answer :( – Wasif Mar 26 '20 at 10:06
  • So if your hash table contains "apple"="delicious", "pear"="yummy", "banana"="meh", you'd want an efficient way to generate string `apple=deliciouspear=yummybanana=meh`, as per the sample code? – vonPryz Mar 26 '20 at 10:12
  • Almost, but a new line after each key-value pair. @vonPryz – Wasif Mar 26 '20 at 10:13
  • 2
    You should put such information in the question body instead of adding comments. If you keep adding information in comments, anyone who reads the question will lack a lot of details. Anyway, you'd expect a single string with line breaks, and not an array of strings? Use StringBuilder. – vonPryz Mar 26 '20 at 10:15
  • A hashtable is a loosely typed (non-generic) collection (both Key and Value), therefore you can't *convert hash tables to key value pairs* as easy as in your example (where the key is presumed a `[String]` and value probably as an `[Int]`). You might want to refine your question as a [`Dictionary Class`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.8). In other words, if you want to keep the full capabilities of the hashtable, you will need to serialize it. – iRon Mar 26 '20 at 10:37

1 Answers1

3

I suggested this script cmdlet for this similar question:

function ConvertTo-StringData {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, Position = 0, ValueFromPipeline)]
        [HashTable[]]$HashTable
    )
    process {
        foreach ($item in $HashTable) {
            foreach ($entry in $item.GetEnumerator()) {
                "{0}={1}" -f $entry.Key, $entry.Value
            }
        }
    }
}

Example:

ConvertTo-StringData $hash 
# or
$hash | ConvertTo-StringData
marsze
  • 15,079
  • 5
  • 45
  • 61