In/Notin operator outputs to collection

Welcome Forums General PowerShell Q&A In/Notin operator outputs to collection

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

js
 
Participant
3 weeks, 2 days ago.

  • Author
    Posts
  • #112741

    Participant
    Points: 0
    Rank: Member

    I'm doing a comparison using the in/notin operator and noticed that if there are duplicate results, it will put the matching values as a collection:

    $list1 | Where-Object Name -in $list2.Name
    

    The results are:

    Name                                                  Application Name
    Application 1                                     {Dragon1,Dragon2,Dragon3}

    Is there anyway to separate them with a switch or another way like this?:

    Name                                                  Application Name
    Application 1                                     Dragon1
    Application 1                                     Dragon2
    Application 1                                     Dragon3

    I can get the same results using a foreach-object and a different operator, but wanted to see if there are a way using the in/notin operator.

  • #112760
    js

    Participant
    Points: 6
    Rank: Member

    Like this?

    $a.'application name' | 
      foreach { [pscustomobject]@{Name=$a.name;'Application Name'=$_} }
    
    Name          Application Name
    ----          ----------------
    Application 1 Dragon1
    Application 1 Dragon2
    Application 1 Dragon3
    
  • #112762

    Participant
    Points: 0
    Rank: Member

    So the code should be this?:

    $a=$list1 | Where-Object Name -in $list2.Name
    $a.'application name' |
    foreach { [pscustomobject]@{Name=$a.name;'Application Name'=$_} }

    Hm...this is the result that I got:

    Name                                                                                        Application Name
    {Dragon1,Dragon2,Dragon3,Pet1,Pet2...}                         Application 1
    {Dragon1,Dragon2,Dragon3,Pet1,Pet2...}                         Application 2
    {Dragon1,Dragon2,Dragon3,Pet1,Pet2...}                         Application 3
    {Dragon1,Dragon2,Dragon3,Pet1,Pet2...}                         Application 4
    {Dragon1,Dragon2,Dragon3,Pet1,Pet2...}                         Application 5
    {Dragon1,Dragon2,Dragon3,Pet1,Pet2...}                         Application 6
    {Dragon1,Dragon2,Dragon3,Pet1,Pet2...}                         Application 7

    Edit, might have to do it this way, but wish it was a little cleaner:

    $a=$list1 | Where-Object Name -in $list2.Name
    $a | ForEach-Object{
    $Name=$_.Name
    $_."Application Name" | ForEach-Object{
    [pscustomobject]@{Name=$Name;'Application Name'=$_}}}
  • #112774

    Participant
    Points: 0
    Rank: Member
    $List1 | 
        Where-Object Name -in $list2.Name |
        Select-Object -Property Name, 'Application Name'

    Assuming I'm reading your code correctly, this should do much the same thing.

    Depends on the collection structure, though.

  • #112780
    js

    Participant
    Points: 6
    Rank: Member

    I thought 'application name' had the list of dragons and 'name' had 'application 1', like in your original post?

    How's this, with 2 loops? Switch the property names around if they're reversed. Other options are to display the whole array. There's some variable that controls it ($formatenumerationlimit = 99). Or turn the array into a string (-join ', ').

    # making an example object array
    $a = [pscustomobject]@{Name='Application 1';'Application Name'='Dragon1','Dragon2',
        'Dragon3'},
      [pscustomobject]@{Name='Application 2';'Application Name'='Dragon1','Dragon2',
        'Dragon3'}
    
    foreach ($row in $a) {
      foreach ($appname in $row.'application name') {
        [pscustomobject]@{Name=$row.name;'Application Name'=$appname}
      }
    }
    
    Name          Application Name
    ----          ----------------
    Application 1 Dragon1
    Application 1 Dragon2
    Application 1 Dragon3
    Application 2 Dragon1
    Application 2 Dragon2
    Application 2 Dragon3
    

You must be logged in to reply to this topic.