How do I create an anonymous object in PowerShell?

后端 未结 3 1222
感动是毒
感动是毒 2021-01-01 12:28

I want to create an object of arbitrary values, sort of like how I can do this in C#

var anon = new { Name = \"Ted\", Age = 10 };
3条回答
  •  情歌与酒
    2021-01-01 13:01

    Try this:

    PS Z:\> $o = @{}
    PS Z:\> $o.Name = "Ted"
    PS Z:\> $o.Age = 10
    

    Note: You can also include this object as the -Body of an Invoke-RestMethod and it'll serialize it with no extra work.

    Update

    Note the comments below. This creates a hashtable.

提交回复
热议问题