create new variable with data from an existing variable

Welcome Forums General PowerShell Q&A create new variable with data from an existing variable

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
2 years, 7 months ago.

  • Author
    Posts
  • #38304

    Participant
    Points: 0
    Rank: Member

    Hi again, so here's a new one, I have a variable that looks like something like this:
    column1 column2 column3 column4
    a1 b1 c1 d1
    a1 b2 c1 d1
    a1 b3 c1 d1
    a2 b3 c1 d1
    a2 b4 c1 d1
    a2 b3 c1 d1
    a3 b5 c1 d1
    a3 b6 c1 d1
    a3 b7 c1 d1
    a4 b5 c1 d1
    I'm trying to create a new variable with just columns 1 and 2 while preserving the order in in the columns, in other words i want to make sure that it will look something like this:
    column1 column2
    a1 b1
    a1 b2
    a1 b3
    a2 b3
    a2 b4
    a2 b3
    a3 b5
    a3 b6
    a3 b7
    a4 b5

    I've already tried:
    $a = @()
    $testdata = New-Object psobject
    $testdata | Add-Member -MemberType NoteProperty -Name Column1 -Value $data.column1
    $testdata | Add-Member -MemberType NoteProperty -Name Column2 -Value $data.column2
    $a += $testdata
    However, instead of getting two nice columns, i get:
    Column1 Column2
    ——- ——-
    {a1,a1,a1,a2,... {b1,b1,b2,b3,...

    Furthermore, once i get the output in desired format, i need to remove duplicates based on column 1, so something like this should be the end result:
    column1 column2
    a1 b1
    a1 b2
    a1 b3
    a2 b3
    a2 b4
    a3 b5
    a3 b6
    a3 b7
    a4 b5
    I haven't gotten to this part yet myself, since i've been banging my head against the wall on the first part.
    Thanks in advance!

  • #38305

    Participant
    Points: 0
    Rank: Member

    ugh, my formatting got all screwed up :-/

  • #38306

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    $new = $old | Select-Object -Prop Column1,Column2

    And you might look further into the -Unique parameter of Select-Object to see if that would be helpful.

  • #38307

    Participant
    Points: 0
    Rank: Member

    Well, now I feel stupid... this is just way too easy... thank you

The topic ‘create new variable with data from an existing variable’ is closed to new replies.