Converting Hash Tables to JSON

by Nov 27, 2017

In previous scripts we worked a lot with hash tables, and even loaded them from .psd1 files. If you need the data in a different format, for example JSON, conversion is trivial. All you need to do is convert a hash table to an object first:

$hash = @{ Name = 'Tobias' ID = 12 Path = 'c:\windows' } $object = [PSCustomObject]$hash $object | ConvertTo-Json 

Once the hash table is an object, you can simply pipe it to ConvertTo-Json, or any of the other ConvertTo-* cmdlets.

Twitter This Tip! ReTweet this Tip!