JBerto
JBerto

Reputation: 263

Powershell file comparison

I'm trying to compare two directories to find any differences between the directories (extra files, missing files, extra directories, etc). The script I am using below generates the output, but what I would like to have the script do is print out the full path of any extra file or extra directory not just the file or directory itself.

So here, Test1 folder has two extra directories and two extra files not present in the Test directory. So in the output I'd like to see the full path not just the names of the directories and files.

Current output -

ExtraDir1

ExtraDir2

Extra file1

Extra file2

Desired output -

/Users/jb/Documents/PowerShell/Test1/ExtraDir1

/Users/jb/Documents/PowerShell/Test1/ExtraDir2

/Users/jb/Documents/PowerShell/ExtraFile1

/Users/jb/Documents/PowerShell/Test1/ExtraDir2/ExtraFile2

$Source = Get-ChildItem -Recurse -path /Users/jb/Documents/PowerShell/Test
$Target = Get-ChildItem -Recurse -path /Users/jb/Documents/PowerShell/Test1

Compare-Object -ReferenceObject $Source -DifferenceObject $Target | %{
if ($_.SideIndicator -eq "=>") {"$($_.InputObject) "}
} >> ./Results/ExtraDir.txt

Upvotes: 0

Views: 82

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174505

Use the FullName property of the input objects from Get-ChildItem to get the full path:

Compare-Object -ReferenceObject $Source -DifferenceObject $Target | %{
  if ($_.SideIndicator -eq "=>") {"$($_.InputObject.FullName) "}
} >> ./Results/ExtraDir.txt

Upvotes: 1

Related Questions