Does Compare-Object return anything if there is an exact match?

Con risposta Does Compare-Object return anything if there is an exact match?

  • Thursday, May 03, 2012 2:15 PM
     
      Has Code

    I am doing some validation on a file/folder copy and want to use Compare-Object to validate the copy.  Using the script listed below I see no result from my Compare-Object.  Am I missing something?  This is v2 by the way.

    md .\difftest1, .\difftest2
    dir > .\difftest1\test1.txt
    dir > .\difftest1\test2.txt
    copy .\difftest1\* .\difftest2
    $dir1 = Get-ChildItem .\difftest1 -Recurse
    $dir2 = Get-ChildItem .\difftest2 -Recurse
    Compare-Object $dir1 $dir2

All Replies

  • Thursday, May 03, 2012 2:21 PM
     
      Has Code
    md .\difftest1, .\difftest2
    dir > .\difftest1\test1.txt
    dir > .\difftest1\test2.txt
    copy .\difftest1\* .\difftest2
    $dir1 = Get-ChildItem .\difftest1 -Recurse
    $dir2 = Get-ChildItem .\difftest2 -Recurse
    Compare-Object $dir1 $dir2 -IncludeEqual


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

  • Thursday, May 03, 2012 2:36 PM
     
      Has Code

    I don't really need to know what matches. I just need to know if they are different.  Testing for $null would be easy enough if the results were by design empty.  So if I store my last line into a variable and test this would work....in theory.

    $diff = Compare-Object $dir1 $dir2
    if($diff)
    {
     "Stuff ain't matchin bud."
    }
    else
    {
     "Congrats you did something right."
    }

  • Thursday, May 03, 2012 2:45 PM
     
     
    By the title of your post, it seemed that's what you were asking.  Now I'm not sure what you're asking.  The code you just posted should work.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

  • Thursday, May 03, 2012 2:51 PM
     
     
    Rephrasing.  If I run Compare-Object (like the last line of my first post) does an empty result mean the two sets are identical? In other words, is nothing an "A OK"?
  • Thursday, May 03, 2012 2:59 PM
     
     Answered
    Absolutely YES.  The result will be equal to $null (I tested it in the meantime).  I think the -Includeequal parameter is meant just to reassure you that it's doing something!

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Marked As Answer by Will Steele Thursday, May 03, 2012 3:03 PM
    •  
  • Thursday, May 03, 2012 3:03 PM
     
     
    Ok. Thanks. I was just doing a sanity check.
  • Friday, May 04, 2012 3:35 AM
     
     

    When all else fails, try help:

    This command compares the contents of two text files. It displays only the lines that appear in one file or in the other file, not lines that appear in both files.

    In other words the activity of comparing is to identify differences, not samenesses.

     


    Al Dunbar

  • Friday, May 04, 2012 12:22 PM
     
     
    ohhhh burn
    lol
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Friday, May 04, 2012 12:29 PM
     
     

    When all else fails, try help:

    This command compares the contents of two text files. It displays only the lines that appear in one file or in the other file, not lines that appear in both files.

    In other words the activity of comparing is to identify differences, not samenesses.


    Al Dunbar


    Unless you specify -Includeequal.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

  • Saturday, May 05, 2012 11:08 PM
     
     

    When all else fails, try help:

    This command compares the contents of two text files. It displays only the lines that appear in one file or in the other file, not lines that appear in both files.

    In other words the activity of comparing is to identify differences, not samenesses.


    Al Dunbar


    Unless you specify -Includeequal.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Of course. The quote I took from help was in reference to the first example command, which did not use that switch parameter.


    Al Dunbar

  • Monday, May 07, 2012 2:07 PM
     
     
    Sorry guys, was out of PC range for a few days. So, for my scenario, I should use the -IncludeEqual switch and then do a -match against <= or =>?
  • Monday, May 07, 2012 3:13 PM
     
     
    Sorry guys, was out of PC range for a few days. So, for my scenario, I should use the -IncludeEqual switch and then do a -match against <= or =>?

    There is no need for all that.  Simply test the result for $null. 

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)