views:

723

answers:

5

Does the latest version of Powershell have the ability to do something like JavaScript's:

var point = new Object();
point.x = 12;
point.y = 50;

If not, what is the equivalent or workaround?

UPDATE
Read all comments

+7  A: 

The syntax is not directly supported by the functionality is there via the add-member cmdlet's. Awhile ago, I wrapped this functionality in a general purpose tuple function.

This will give you the ability to one line create these objects.

$point = New-Tuple "x",12,"y",50

Here is the code for New-Tuple

function New-Tuple()
{
    param ( [object[]]$list= $(throw "Please specify the list of names and values") )

    $tuple = new-object psobject
    for ( $i= 0 ; $i -lt $list.Length; $i = $i+2)
    {
        $name = [string]($list[$i])
        $value = $list[$i+1]
        $tuple | add-member NoteProperty $name $value
    }

    return $tuple
}

Blog Post on the subject: http://blogs.msdn.com/jaredpar/archive/2007/11/29/tuples-in-powershell.aspx#comments

JaredPar
+1 I like this. You know... before I asked this I should have thought about just using a Powershell hashtable. But very cool approach.
tyndall
Thanks. Just did some reading on psobject, add-member, NoteProperty ... cool stuff. Learned something today :)
tyndall
+1  A: 

You can do it like this:

$point = New-Object Object |
    Add-Member NoteProperty x ([int] 12) -passThru |
    Add-Member NoteProperty y ([int] 15) -passThru

Regarding one of your comments elsewhere, custom objects may be more useful than hash tables because they work better with cmdlets that expect objects to have named properties. For example:

$mypoints | Sort-Object y   # mypoints sorted by y-value
dangph
+2  A: 

How to Create an Object in PowerShell
http://blogs.msdn.com/powershell/archive/2009/03/11/how-to-create-an-object-in-powershell.aspx

Shay Levy
Yes, James gives us most of the answers. There is another simple option for classes that are just property bags: $Object = "" | Select Prop1,Prop2,Prop3
JasonMArcher
There is also this function that converts a hashtable into an object: http://powershell.com/cs/blogs/tips/archive/2008/11/14/converting-hash-tables-to-objects.aspx
JasonMArcher
@JasonMArcher: I +1'd the answer but really mean to post your tip - for me its neater than the accepted and I used it. Now to find another post of yours to +1 instead!
Ruben Bartelink
In PowerShell v2, there is built in support for creating objects from Hashtables with New-Object PSObject -Property $hashtable
JasonMArcher
+2  A: 

Sorry, even though the selected answer is good, I couldn't resist the hacky one line answer:

New-Object PsObject | Select-Object x,y | %{$_.x = 12; $_.y = 50; $foo = $_; }
EBGreen
Personally I like the multiassignment syntax:... | %{$_.x,$_.y,$foo = 12,50,$_ }
stej
+4  A: 

For simple ways, first, is a hashtable (available in V1)

$obj = @{} $obj.x = 1 $obj.y = 2

Second, is a PSObject (easier in V2)

$obj = new-object psobject -property @{x = 1; y =2}

It gives you roughly the same object, but psobjects are nicer if you want to sort/group/format/export them

mrwaim
Very good answer. +1
tyndall