Boost your shell with reusable code stripped to its essentials


PowerShell has a few cmdlets to convert (complex) objects to a string. This is called serialization or stringification. Examples are:

Besides there are a few cmdlets to do the opposite and convert the string back to a (complex) object. This is called deserialization or parsing. Examples are:

But there is no standard cmdlet that lets you convert an object to a PowerShell Object Notation (PSON), yet there exists actually already a kind of ConvertFrom-PSON in the form of: Invoke-Expression. The Invoke-Expressioncmdlet evaluates or runs a specified string as a command and returns the results of the expression or command. This means that the only thing required is a cmdlet which can do the opposite: serialize an object to a PSON string that can be read back with Invoke-Expression (which could be aliased to: ConvertFrom-PSON). At the same time, Invoke-Expression is a good praktisch to test the integrity of a ConvertTo-Expression (alias ConvertTo-PSON) cmdlet; it shouldn’t produce any error, all (embedded) objects and values should be read back and the datatypes should match the source as much as possible. Note here that not all datatypes can be converted and converted back, simply because you can’t assume that the required constructor exists on every other system. Due to this, system objects are converted to PSCustomObject objects which usually react the same with regards property references. Saying that, in general, PSON is better capable of handling native PowerShell object types as e.g. [Boolean], [Char] and [DateTime] than JSON, but the downside to this is that it can’t interchange data with other programming languages (yet?). An other advantage of PSON is that Invoke-Expression is also available in older PowerShell versions were the ConvertTo-Expression cmdlet can easily be added as well.

Besides serializing objects to a string, the ConvertTo-Expression cmdlet can come in handy for revealing and logging datatypes and structures of (embedded) objects and values.

Last updated on 29 Jun 2020
Published on 6 Nov 2017