Syntax error How to convert Dictionary to Hashtable in PowerShell?

How to convert Dictionary to Hashtable in PowerShell?



Like any other data type conversion in PowerShell, we can convert Dictionary to hashtable in a similar way.  We have a below Dictionary for the example called $CityData.

Key     Value
---     -----
India      91
Austria    43

Its datatype is Dictionary,

Example

PS C:\> $citydata.GetType() | ft -AutoSize

Output

IsPublic IsSerial Name         BaseType
-------- -------- ----         --------
True     True     Dictionary`2 System.Object

To convert it to the hashtable,

$hash = [Hashtable]$citydata

Or

$hash = [System.Collections.Hashtable]$CityData

Datatype:

PS C:\> $hash | ft -AutoSize

Output

Name    Value
----    -----
Austria 43
India   91
Updated on: 2020-12-15T07:54:24+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements