Combining Arrays

Mike 246 Reputation points
2022-03-18T21:15:53.57+00:00

If I have an this table

"Email","Compliance","DisplayName","Devicename"
"pmichael@globalpsa.com","noncompliant","John Doe","HOMETEST-VM"

and I have another one that looks like this
"Email","Domain"
pmichael@globalpsa.com,Microsoft.com

How do I make it like this
"Email","Compliance","DisplayName","Devicename","Domain"
"pmichael@globalpsa.com","noncompliant","John Doe","HOMETEST-VM",Microsoft.com

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Naw 1 Reputation point
    2022-03-18T23:49:38.807+00:00
    $FirstArray      = Import-Csv c:\temp\FirstArry.csv
    $SecondArray = Import-Csv C:\temp\SecondArry.csv
    
    Foreach($Row in $FirstArray){
        Foreach($Item in $SecondArray){
            If ($Row.Email -eq $Item.Email){
                $Row | Select Email,Compliance,DisplayName,Devicename,@{n='Domain';e={$Item.Domain}}  
            }
        }
    }
    

    Hope this help!

    0 comments No comments

  2. Rich Matheisen 45,906 Reputation points
    2022-03-19T01:56:52.313+00:00

    Here's another way to handle this:

    $hash = @{}  
    Import-CSV C:\Junk\EmailDomain.csv |  
        ForEach-Object{  
            $hash[$_.Email] = $_.Domain  
        }  
    Import-Csv c:\junk\EmailCompliance.csv |  
        ForEach-Object{  
            $_ | Add-Member NoteProperty 'Domain' ""  
            if ($hash.ContainsKey($_.Email)){  
                $_.Domain = $hash[$_.Email]  
            }  
            $_  
        } | Export-Csv C:\Junk\Combined.csv -NoTypeInformation  
    

    You'll only make one pass through each CSV this way. Using the technique offered by @Naw the code makes one pass through the 1st CSV but would make as many passes through the 2nd CSV as there are rows in the 1st CSV. For small data sets that my not amount to much, but if there were 1000 rows in each CSV you'd make 1,000,000 passes over the 2nd CSV!

    0 comments No comments

  3. Mike 246 Reputation points
    2022-03-19T06:52:11.983+00:00

    If my table is already in a variable, why it doesn't work if I do it like this

    $users |
    ForEach-Object{
    $hash[$.Email] = $.Domain
    }


  4. Mike 246 Reputation points
    2022-03-19T13:52:06.447+00:00

    sorry my bad. I did it this way

    $users |
    ForEach-Object{
    $_ | Add-Member NoteProperty 'Domain' ""
    if ($hash.ContainsKey($.Email)){
    $
    .Domain = $hash[$.Email]
    }
    $

    } | Export-Csv C:\Junk\Combined.csv -NoTypeInformation

    But I coudn't make it work


  5. Rich Matheisen 45,906 Reputation points
    2022-03-19T15:15:37.863+00:00

    If what you want to do (without using CSV files) is to add a "Domain" member to existing PSCustomObjects in an array, then this should work (it's just a slight modification of the code I posted earlier):

    $EmailDomain = @()
    $EmailCompliance = @()
    $hash = @{}
    $EmailDomain |
        ForEach-Object{
            $hash[$_.Email] = $_.Domain
        }
    $EmailCompliance |
        ForEach-Object{
            $_ | Add-Member NoteProperty 'Domain' ""
            if ($hash.ContainsKey($_.Email)){
                $_.Domain = $hash[$_.Email]
            }
        }
    
    0 comments No comments