45

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 };
Luke Puplett
  • 42,091
  • 47
  • 181
  • 266

3 Answers3

53

You can do any of the following, in order of easiest usage:

  1. Use Vanilla Hashtable with PowerShell 5+

    In PS5, a vanilla hash table will work for most use cases

    $o = @{ Name = "Ted"; Age = 10 }
    

  2. Convert Hashtable to PSCustomObject

    If you don't have a strong preference, just use this where vanilla hash tables won't work:

    $o = [pscustomobject]@{
        Name = "Ted";
        Age = 10
    }
    

  3. Using Select-Object cmdlet

    $o = Select-Object @{n='Name';e={'Ted'}},
                       @{n='Age';e={10}} `
                       -InputObject ''
    

  4. Using New-Object and Add-Member

    $o = New-Object -TypeName psobject
    $o | Add-Member -MemberType NoteProperty -Name Name -Value 'Ted'
    $o | Add-Member -MemberType NoteProperty -Name Age -Value 10
    

  5. Using New-Object and hashtables

    $properties = @{
        Name = "Ted";
        Age = 10
    }
    $o = New-Object psobject -Property $properties;
    

Note: Objects vs. HashTables

Hashtables are just dictionaries containing keys and values, meaning you might not get the expected results from other PS functions that look for objects and properties:

$o = @{ Name="Ted"; Age= 10; }
$o | Select -Property *

Further Reading

KyleMit
  • 30,350
  • 66
  • 462
  • 664
35

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.

Luke Puplett
  • 42,091
  • 47
  • 181
  • 266
  • 10
    I like do it like this: `$o = [ordered]@{ Name = "Ted"; Age = 10 }` – CB. Mar 18 '16 at 10:41
  • 3
    @CB. might want to clarify what `[ordered]` is for in that instance. – JoeBrockhaus Jun 28 '16 at 15:08
  • The "[ordered]" is a type accelerator; it's new as of PowerShell 3.0. It's just saying the thing to the right is an [OrderedDictionary](https://stackoverflow.com/q/7802406/530545). [You don't need it, but it's nice](https://blogs.technet.microsoft.com/heyscriptingguy/2014/09/30/use-powershell-to-create-ordered-dictionary/). – Granger Jan 26 '18 at 15:27
  • 7
    It's not custom object. It's a hashtable, check this `$o.GetType()` out. And in strict PS mode it won't work, only `$o['Name']`. – Vasyl Zvarydchuk Dec 24 '18 at 04:41
  • 1
    PLEASE beware that [the answer by prampe](https://stackoverflow.com/a/59107772/177710) shows how to do this in almost the exact same syntax the OP hoped for: `$anon = @{ Name="Ted"; Age= 10; }`. This is possible from **PowerShell 5** upwards. – Oliver Apr 15 '20 at 11:20
  • As many noted... this creates an hashtable and not a object... the above answer by @KilyMit gaves the correct answer – ZEE Nov 26 '20 at 21:01
  • I've finally got around to switching to the correct answer! Sorry! – Luke Puplett Nov 28 '20 at 12:04
7

With PowerShell 5+ Just declare as:

    $anon = @{ Name="Ted"; Age= 10; }

Example

elpezganzo
  • 349
  • 4
  • 7