1

I want to keep hash table contents in a text file and then load it in new sessions. I was thinking of just storing them in a text file $env:TEMP\myhash.txt as

xxx=123
yyy=234

I want to be able to update the contents of the hash in memory and the hash in the file (then those entries will load in the next session):

$myhash = @{}
function Add-HashEntry ($newkey, $newval) {
    try { $myhash.Add( $newkey, $newval ) } catch { "already in hash" }   # First add it to the hash in memory
    $AddToFile = "$newkey=$newval"                                        # Now save the key/value to file
    $check = Select-String -Quiet -Pattern $AddToFile -Path $env:TEMP\myhash.txt
    if (-not $check) { Add-Content -Path $env:TEMP\myhash.txt -Value $AddToFile }   # "$mykey = $myval" >> $env:TEMP\myhash.txt
}

To load my hash from the file, I might use:

function Load-Hash {
    foreach ($i in Get-Content $env:TEMP\myhash.txt) {
        $key = ($i -split "=")[0] ; $val = ($i -split "=")[1]
        $myhash.Add( $key, $val )   # Note: "Add (" not allowed, must use "Add("
    }
}           

This works, but my question is, are there more efficient ways to achieve this goal? i.e. better ways to store and retrieve the data from the file (maybe using json/xml or other better format) or ways that are more efficient to achieve the goal of storing and loading a hash table from a file than my hackish split by "=" and load the values method? Maybe even there is a way to load a file as a hash table directly etc?

YorSubs
  • 3,194
  • 7
  • 37
  • 60
  • 1
    Why not use Export-CliXML and Import-CliXML [cmdlets](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-clixml?view=powershell-7) instead? – vonPryz Mar 19 '20 at 06:59
  • I think so, yes, I had no idea about this functionality, quite exciting really. So I can load up a hash table `$x = @{x=111;y=222}`, then export `$x | export-clixml $env:TEMP\myhash.xml` and I can import that again instantly. Amazing. :). Is there a way to append to the serialized output by the way? Each time I do `$x | export-clixml ` it will overwrite the file. That's fine for a small amount of data but if just adding one value to a large amount of data `$x.Add("zzz","000")` say that might be ineffecient. – YorSubs Mar 19 '20 at 07:30
  • If it only concerns `[String]` values, you might also have a look at: [ConvertFrom-StringData](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/convertfrom-stringdata?view=powershell-5.1) – iRon Mar 19 '20 at 07:30
  • As you will need to search the whole table anyways and not only append (but probably also replace if the key is equal but the value different, or not?).Therefore I would load the whole data (hashtable) into memory and add/replace the concerned key `$MyHash[$Key] = ...` and simply overwrite the (whole) configuration file. – iRon Mar 19 '20 at 07:43
  • For my current situation this will be perfect. I was also thinking about cases where I might have 10's of MB of data as it sounds ineffecient to read a 20 MB file, then update one key, then write all back to file as XML so I'm curious how you might handle that situation? `ConvertFrom-StringData` is also new to me and also great, though I'll probably use `Import/Export-CliXML` as it's so generically useful for any data (thanks @vonPryz / @iRon ! :) ). – YorSubs Mar 19 '20 at 08:36

0 Answers0