Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save hash table in PowerShell object notation (PSON)

The question Loading a PowerShell hashtable from a file? documents how to load a file that contains a hashtable in PSON format into a variable, but how does one save a hashtable to a file in PSON format?

Hashtable:

@{            
 "name" = "report 0"            
 "parameters" = @(
    @{"name" = "parameter 0"; "default" = 1; "values"=1,2,3,4},
    @{"name" = "parameter 1"; "default" = 'A'; "values" = 'A','B','C'}
    )            
}
like image 485
craig Avatar asked Feb 28 '13 15:02

craig


People also ask

How do I add a hash table in PowerShell?

And, you can add keys and values to a hash table by using the addition operator ( + ) to add a hash table to an existing hash table. For example, the following statement adds a "Time" key with a value of "Now" to the hash table in the $hash variable. You can also add values that are stored in variables.

How does PowerShell define hash table?

Generally, you think of a hashtable as a key/value pair, where you provide one key and get one value. PowerShell allows you to provide an array of keys to get multiple values. In this example, I use the same lookup hashtable from above and provide three different array styles to get the matches.

How do I clone a Hashtable in PowerShell?

Copying Hashtables (For Real) For a one-level hashtable like the above example, use the . Clone() method. This method creates a new hashtable object and assigns it to a variable. Let's create $person3 by cloning $person1 , then follow this up by adding a new property to $person3 .

What is @{} in PowerShell?

@{} in PowerShell defines a hashtable, a data structure for mapping unique keys to values (in other languages this data structure is called "dictionary" or "associative array"). @{} on its own defines an empty hashtable, that can then be filled with values, e.g. like this: $h = @{} $h['a'] = 'foo' $h['b'] = 'bar'


Video Answer


2 Answers

After 5 years, the cmdlet I had pasted in the original answer has undergone so many updates that it has become completely outdated. Therefore I have replaced the code and the ReadMe with a link to the latest version.

Besides the support for more object types and better formatting, it now outputs an expression (ScriptBlock), which default display type is still a String and allows for direct invocation using the call operator &:

$Object = &($Object | ConverTo-Expression)

ConvertTo-Expression

The ConvertTo-Expression cmdlet can be download from PowerShell Gallery using the command:

Install-Script -Name ConvertTo-Expression

ReadMe

The full ReadMe (and source code) is available from GitHub:
https://github.com/iRon7/ConvertTo-Expression

Installation

After downloading (Install-Script -Name ConvertTo-Expression), the script can simply be invoked by dot sourcing:

. .\ConvertTo-Expression.ps1

You might also consider to convert the script to a PowerShell module by renaming it to a PowerShell module (.psm1) file and moving it to a one of the module folders defined in $env:PSModulePath. For more details see: How to Write a PowerShell Script Module.

Answer

Below are some possible options to serialize the specific example (assigned to $Craig) in the question:

ConvertTo-Expression $Craig
@{
    parameters =
        @{
            name = 'parameter 0'
            default = 1
            values =
                1,
                2,
                3,
                4
        },
        @{
            name = 'parameter 1'
            default = 'A'
            values =
                'A',
                'B',
                'C'
        }
    name = 'report 0'
}

To limit the tree view expansion:
(Expand -0 will output a single line and Expand -1 will remove also the unnecessary spaces)

ConvertTo-Expression $Craig -expand 3
@{
    parameters =
        @{name = 'parameter 0'; default = 1; values = 1, 2, 3, 4},
        @{name = 'parameter 1'; default = 'A'; values = 'A', 'B', 'C'}
    name = 'report 0'
}

Preserving the explicit types (strong typed):

ConvertTo-Expression $Craig -expand 3 -Strong
[hashtable]@{
    parameters = [array](
        [hashtable]@{name = [string]'parameter 0'; default = [int]1; values = [array]([int]1, [int]2, [int]3, [int]4)},
        [hashtable]@{name = [string]'parameter 1'; default = [string]'A'; values = [array]([string]'A', [string]'B', [string]'C')}
    )
    name = [string]'report 0'
}

(Note: As per PowerShell design, HashTables are not in order, but if required you might use the [Ordered] type instead.)

like image 104
iRon Avatar answered Oct 19 '22 06:10

iRon


Try the *-CliXml cmdlets. To save the object:

@{            
 "name" = "report 0"            
 "parameters" = @(
    @{"name" = "parameter 0"; "default" = 1; "values"=1,2,3,4},
    @{"name" = "parameter 1"; "default" = 'A'; "values" = 'A','B','C'}
    )            
} | Export-Clixml -Path c:\hash.xml

To read it back:

Import-Clixml c:\hash.xml
like image 44
Shay Levy Avatar answered Oct 19 '22 06:10

Shay Levy