Why using JSON Files in PowerShell is neat

Timm Brochhaus's picture

In the last couple of weeks I worked a lot with PowerShell and text-based input files to separate code and data. Since my beginnings in PowerShell I always used XML-based files to cover the separation. 

The task I am currently working on is very complex and the designed XML data structure is not readable in an editor like notepad++ anymore. Due to this I had to search for an alternative file format. Some colleagues of mine are currently involved in Microsoft Azure projects and I heard about the JSON file format from them. JSON stands for JavaScript Object Notation (http://en.wikipedia.org/wiki/JSON) and is a well-known format within Azure. JSON provides a set of supported data types you can use directly (like Number, String, Boolean, Array, …) what is very handy and easy to adopt. 

During my research and testing I found a lot of helpful websites and blogs covering JSON handling within PowerShell. Within this article I want to share some of my experiences with you.


First, let’s have a look at a simple JSON file:

    "Name": "Timm-TestCatalog-002",
    "Description": "Timm-TestCatalog-002",
    "AllocationType": "Random",
    "ProvisioningType": "Manual",
    "SessionSupport": "MultiSession",
    "PersistUserChanges": "OnLocal",
    "ProvisioningSchemeId": "",
    "CatalogKind": "",
    "PvsForVM": "",
    "IsRemotePC": false,
    "MachinesArePhysical": true,
    "MinimumFunctionalLevel": "L7_6",
    "PvsAddress": "",
    "PvsDomain": "",
    "RemotePCHypervisorConnectionUid": "",
    "Scope": [

As you can see within this example – description of a XenDesktop Catalog - I use different data types (String, Boolean, Array). 

Now I can easily import this JSON file into PowerShell with the following command one-liner:

$JSONFile = ConvertFrom-Json "$(get-content $(Join-Path $env:temp "File.json"))"

Within the PowerShell $JSONFile is a PSCustomObject:


IsPublic 	IsSerial 		Name			   BaseType
-------- 	-------- 		----			   --------
True     	False    		PSCustomObject		   System.Object

And the NoteProperty “IsRemotePC” is a Boolean – as expected:


IsPublic 	IsSerial 		Name			BaseType                                                                                                                           
-------- 	-------- 		----			--------                                                                                                                           
True     	True     		Boolean			System.ValueType 

Now I can use these information to create a new catalog within XenDesktop. To do this, transform the PSCustomObject into a Hash Table, which can be provided directly to the Citrix XenDesktop cmdlet (see my previous post covering parameters and Hash Tables):

$HashTable = @{}

$JSONFile | get-member -MemberType NoteProperty | Where-Object{ -not [string]::IsNullOrEmpty($JSONFile."$($_.name)")} | ForEach-Object {$HashTable.add($_.name,$ JSONFile."$($_.name)")}

$BrokerCatalog = New-BrokerCatalog @HashTable 


As you see working with JSON files within PowerShell is very neat. The example I shared with you is not that difficult, sure. Within my PowerShell scripts I am currently working with JSON file with more than 200 lines and multiple sections resulting in different Hash Tables. I will share more examples with you in one of my following articles.

Add new comment
By submitting this form, you accept the Mollom privacy policy.