Adding Results from Compare-Object to Array

Adding Results from Compare-Object to Array

powershell compare-object array
powershell compare multidimensional arrays
powershell compare two lists
powershell compare-object alternative
powershell compare array content
powershell compare files
powershell compare arrays for equality
powershell compare-object hashtable

I have two files being compared, one a CSV and one a txt file. The CSV file has the information for around 5,000 servers and the txt file has only server names for around 3,000. The columns within the CSV are Name, OS, and Type. This is what I did to compare the objects:

$compareObject = Compare-Object -ReferenceObject $txtFile -DifferenceObject $csvFile.Name -IncludeEqual

After this, I was then given three options. Ones that are on both lists ==, ones that are only on the txt file =>, and ones that are only on the csv file =<.

What I'm trying to do is take the .SideIndicator for values that equal ==, and put that as a column within the $csvFile so I can eventually do If ($csvFile.SideIndicator -eq "==")...

So basically I'm trying to figure out how to write:

If (($csvFile.Name -like $compareObject.InputObject) -and ($compareObject.InputObject -eq "==") {
    (add .SideIndicator to CSV file)

I've tried placing a variable $count++ where I currently have add .SideIndicator... within my script to see how many results return, and it always returns 0.

Can someone help me out with this or give me some ideas?

You want to pipe the results of Compare-Object to endable the filtering of SideIndicator like so:

$fields = "name", "street", "zip", "city"
Compare-Object -ReferenceObject $txtFile -DifferenceObject $csvFile.Name -IncludeEqual -Property $fields -PassThru | Where-Object {
    $_.SideIndicator -eq "=="
} | Select-Object $fields | Export-Csv ".\output.csv" -NoTypeInformation

Where $fields contain your CSV-Headers

Compare-object when comparing an array, $Different=Compare-Object $Reservations $DHCP01. #$Different | ForEach-​Object{. #Add-DhcpServerv4Reservation -ClientId $_.ClientId -IPAddress $_. Using the Compare-Object Cmdlet You can also use PowerShell to compare arrays using the Compare-Object cmdlet. This cmdlet takes a reference object and a difference object and returns a side indicator indicating which elements are and are not in either array.

This may be way too complicated, but here you go:

Suppose this is your CSV file:


and this is the content of your text file:


Then this code:

$csvFile = 'D:\blah.csv'
$txtFile = 'D:\names.txt'

# import the .csv file
$csv = Import-Csv -Path $csvFile
# read the .txt file which contains only server names, each on a separate line
$txt = Get-Content -Path $txtFile

$items = Compare-Object -ReferenceObject $txt -DifferenceObject $csv.Name -IncludeEqual -PassThru 
$result = foreach ($item in $items) {
    $name = $item.ToString()
    switch ($item.SideIndicator) {
        '<='  { 
            # the servername is only present in the text file
                Name = $name
                OS = ''
                Type = ''
                SideIndicator = $item.SideIndicator
                Comment = "Name only found in $txtFile"
        default  { 
            # '==' AND '=>': the servername is only present in the csv file or in both files
            $server = @($csv | Where-Object { $_.Name -eq $name })[0]
                Name = $server.Name
                OS = $server.OS
                Type = $server.Type
                SideIndicator = $item.SideIndicator
                Comment = if ($item.SideIndicator -eq '==') { "Name found in both files" } else { "Name only found in $csvFile" }
$result | Format-Table -AutoSize

produces this result:

Name     OS      Type                SideIndicator Comment                        
----     --      ----                ------------- -------                        
wddg9028 Windows WindowsAutherServer ==            Name found in both files       
htew804  Windows WindowsAutherServer ==            Name found in both files       
other321 Windows WindowsBackupServer =>            Name only found in D:\blah.csv 
test1234                             <=            Name only found in D:\names.txt

If you want this info to be written to a new CSV file, change the Format-Table -AutoSize into Export-Csv -Path 'D:\blah_updated.csv' -NoTypeInformation

Compare-Object, The Compare-Object cmdlet compares two sets of objects. The result of the comparison indicates whether a property value appeared only in the but the instance of the object returned has an added NoteProperty named SideIndicator. Specifies an array of properties of the reference and difference objects to compare. A late comment … it might be obvious, but it is not the data type but the operation which makes the difference.. you can declare a System.Collections.ArrayList and add items with += and the times are much the same as when you use += with the default array.

Measure-Object, The Measure-Object cmdlet performs calculations on the property values of objects. You can also use Measure-Object to calculate the Minimum, Maximum​, Sum, Without the Raw parameter, Get-Content outputs the file as an array of lines. Compare-Object · ForEach-Object · Group-Object · New-Object · Select-​Object  This was a perfect opportunity. Not only would I get the results I needed — the difference in access rights — but I also got a half of minute of working with the Compare-Object cmdlet and the .Replace() and .Split() methods, where I may not have otherwise. The more I use PowerShell, the less I have to try and remember when I really need it.

Split array of objects by property based on Compare-Object results , If so, add a "$variable =" before the foreach and all the resulting objects will be an object in a variable for you. Stack Overflow Public questions and Adding Results from Compare-Object to Array. You want to pipe the results of Compare-Object to endable the filtering of

Powershell compare-object select results from both sides, Powershell compare-object select results from both sides · powershell files. I have read many example of comparing two objects, lists, arrays etc. So the simple approach is to join all the elements in the array into a single string and compares that. This assumes that the elements of the array are in the same order. That is, if in the first item Office10 was the first element of the array and Office13 was the second, and then in the second item they were reversed, this simple method would

array_diff - Manual, array. Compares array1 against one or more other arrays and returns the values in array_diff_assoc() - Computes the difference of arrays with additional index check The same problem you will notice when checking with objects inside your array. I hope you find it useful, and I encourage PHP "makers", to add in a not  PT, that is all there is to modifying values in an array, adding to an array, checking to see if an array contains a specific value, and sorting the array. Array Week will continue tomorrow when I will store different types of objects in an array (including other arrays). It will be fun and educational. See ya!