1

I have a PS object and couldnt figure out a way to append values to my object.

$object = New-Object PSObject
Add-Member -InputObject $object -MemberType NoteProperty -Name Col1 -Value ""
Add-Member -InputObject $object -MemberType NoteProperty -Name Col2 -Value ""
Add-Member -InputObject $object -MemberType NoteProperty -Name Type -Value ""
1..10 |ForEach{
    $src=$_
    11..20 | ForEach{
        $dst = $_
        $object.Col1=$src
        $object.Col2=$dst
        $object.Type="New"
    }    
}

I want my result like

col1  col2  Type
----  ----  ----
   1    11  New
   1    12  New
   1    13  New
   1    14  New
...
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user1550159
  • 1,197
  • 3
  • 19
  • 36

2 Answers2

2

Use a PSCustomObject:

$values = 1..10 | % { 
   [pscustomobject]@{ col1=1; col2=10+$_; Type="New" }
   }
Burt_Harris
  • 6,415
  • 2
  • 29
  • 64
  • Thanks, how do i save that to an object and use it some where in my script.I tired saving it an object, but it saved only last element. – user1550159 Sep 15 '16 at 21:54
  • `PSOjbect` or `PSCustomObject` doesn't make a difference (unless you want to use a type accelerator). The point is to create (and output) new objects *inside* the loop. The list can be captured by assigning the pipeline output to a variable: `$list = 1..10 | % {...}` – Ansgar Wiechers Sep 15 '16 at 22:11
  • I've updated the example to save into variable $values. I get different results @AnsgarWiechers, try it yourself replacing `pscustomobject` with `psobject`. – Burt_Harris Sep 15 '16 at 22:44
  • You're using a [type accelerator](http://stackoverflow.com/q/35894272/1630171), so yes, you're getting different results. Use `New-Object` and the results will be the same. – Ansgar Wiechers Sep 15 '16 at 22:49
  • @AnsgarWiechers, yes but try this: `New-Object -Type PSObject | GM`. It returns a PSCustomObject even though you ask for a PSObject. Special cases abound in this area. – Burt_Harris Sep 15 '16 at 22:54
  • Like I said: it only makes a difference when you use type accelerators. – Ansgar Wiechers Sep 15 '16 at 23:01
  • And from the beginning my answer has been using a type accelerator. Putting a comment in that PSObject vs PSCustomObject doesn't matter in the context of my answer is likely to confuse people into typing the shorter one, which won't work. – Burt_Harris Sep 15 '16 at 23:27
  • *\*sigh\** Again, the relevant point is to create objects **inside** the loop, not to use `PSCustomObject`, **which the OP was using anyway**. If something is misleading it's the introductory *"Use a PS**Custom**Object:"* in your answer. That is what my comment addressed. HTH. HAND. – Ansgar Wiechers Sep 16 '16 at 00:59
  • Thanks a lot Ansgar and Harris. Both solutions worked as expected. I am not an expert in PS ,but it seems we are creating new objects with in loop instead of creating an object at beginning and adding elements to it. Is there any way to do that , more like how add in HASHTABLE $states.Add("Alaska", "Fairbanks"). – user1550159 Sep 16 '16 at 13:40
  • @user1550159 You could do that by creating an object with array properties, and appending the individual values to the properties. I wouldn't recommend going that route, though. It's awkward, and it won't give you the output you want. Creating a list of objects is how you do this kind of thing in PowerShell. – Ansgar Wiechers Sep 17 '16 at 19:08
1

The output you want is a list of objects, not a single object. You generate that by creating the objects inside the loop. @Burt_Harris already showed you one way to do that (using a type accelerator [PSCustomObject]), but of course you can also use New-Object to the same end:

$list = 1..10 | ForEach-Object {
    $src = $_
    11..20 | ForEach-Object {
        $prop = @{
            Col1 = $src
            Col2 = $_
            Type = 'New'
        }
        New-Object -Type PSObject -Property $prop
    }
}

Create the property hashtable as an ordered hashtable if you want the properties to appear in a particular order in the output (PowerShell v3 and newer only):

$prop = [ordered]@{
    Col1 = $src
    Col2 = $_
    Type = 'New'
}

The list of objects can be captured by assigning the pipeline output to a variable ($list = ...).

Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328