1

I'm working on a script that compares the content of two directories with files of the same name and returns files that are different. The property I'm using to determine if the files are different are LastWriteTime and Size.

Here is my current script:

$var1 = Get-ChildItem -path C:\proj\BIN\ -Recurse 
$var2 = Get-ChildItem -path C:\proj2\BIN\ -Recurse 
Compare-Object -ReferenceObject $var1 -DifferenceObject $var2 -Property Name, LastWriteTime, Size | Sort-Object Name

Here is my current output:

Name          LastWriteTime          Size SideIndicator
----          -------------          ---- -------------
folder1       6/1/2018 3:20:37 PM         =>
folder1       5/21/2018 12:08:36 PM       <=
folder2       6/1/2018 3:20:37 PM         =>
folder2       5/21/2018 12:08:36 PM       <=
CustomReports 6/1/2018 3:20:37 PM         =>
CustomReports 5/21/2018 12:08:39 PM       <=
folder3       6/1/2018 3:20:38 PM         =>
folder3       5/21/2018 12:08:38 PM       <=
blank.XML     6/1/2018 3:37:49 PM         =>
blank.XML     11/22/2017 12:36:04 PM      <=

As far as I know, the reason size is empty is because they are the same size (which they are). If this is true, no problem. If it happens to be just a coincidence, then I will need to correct it.

EDIT: size is incorrect. length is the correct property.

I want to change the output from just Name to the Fullname. If I change my it in my script it will list every file because they are in different directories.

I have added this pipe to the last line to this instead:

| Format-Table -property FullName, LastWriteTime, Size

From my understanding it the script should compare-object based off name, lastWriteTime, size and from those results sort-object by name and then return the fullname, lastWriteTime, size from those results.

My new output is:

FullName LastWriteTime          Size
-------- -------------          ----
         6/1/2018 3:20:37 PM
         5/21/2018 12:08:36 PM
         6/1/2018 3:20:37 PM
         5/21/2018 12:08:36 PM
         6/1/2018 3:20:37 PM
         5/21/2018 12:08:39 PM
         6/1/2018 3:20:38 PM
3
  • 2
    [System.IO.FileInfo] instances don't have a .Size property, only a .Length property; [System.IO.DirectoryInfo] instances (directories aka folders) have neither. Commented Jun 1, 2018 at 21:20
  • 1
    Thanks! Implemented it and saw the change instantly. Commented Jun 1, 2018 at 21:24
  • 3
    Glad to hear it; when in doubt, pipe a command's output to Get-Member to inspect its members (properties and methods). Commented Jun 1, 2018 at 21:26

2 Answers 2

2

If you use -Property with Compare-Object, the output objects only have the specified properties.

To pass the input objects through[1] , add the -PassThru switch.

In your case that should make the .FullName property available on the output from Compare-Object / Sort-Object.

To put it all together:

Compare-Object $var1 $var2 -PassThru -Property Name, LastWriteTime, Length | 
  Sort-Object Name | 
    Format-Table FullName, LastWriteTime, Length

[1] With -PassThru the objects being passed through are augmented with a .SideIndicator NoteProperty member (a property with a static value added by PowerShell), so that you can still filter the output objects by what side (-ReferenceObject vs. -DifferenceObject) a given object was unique to.

Sign up to request clarification or add additional context in comments.

Comments

1

Rather than try and compare on multiple properties, why not just compare on the hash of the contents. Something like this:

$var1 = Get-ChildItem -path C:\proj\BIN\ -Recurse -File | Get-FileHash -Algorithm SHA1 | Sort Hash
$var2 = Get-ChildItem -path C:\proj2\BIN\ -Recurse -File | Get-FileHash -Algorithm SHA1 | Sort Hash

Compare-Object -ReferenceObject $var1 -DifferenceObject $var2 -Property Hash, {Split-Path $_.Path -leaf} -PassThru

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.