r/PowerShell • u/TTwelveUnits • Nov 22 '24
Question Hashtable syntax
why is it when i declare as hashtable, I can access its properties like an object?
PS C:\Users\john> $obj = @{
>> Name = "John"
>> Age = 30
>> }
PS C:\Users\john> $obj.Name
Johnis this just syntactical sugar, or something? thought i would have to do this:
$obj[Name]
23
Upvotes
View all comments
Show parent comment
1
u/eightbytes Nov 22 '24
I use [ordered]@{} for parameter splatting. Since PSv4, I can pass parameters with strict ordering. For the OP's question, that's some magic PS give us. The [hashtable] and [ordered] types are translated into [psobject] with name-value pairs converted into properties. Same thing with JSON objects and [xml] upto some point.