Search code examples
powershelldynamiccompareobject

Compare-Object Dynamic Variables for ReferenceObject DifferenceObject


This Compare-Object doesn't work, but is there a way to do the same thing?

For ($F=0; $F -le $NumberOfSameFolders; $F++)
{
$Results$F = Compare-Object "$FilesInFolderX$F" "$FilesInFolderY$F" 
-Property Name, LastWriteTime -IncludeEqual -PassThru | 
Select-Object FullName
}

This is an experiment that works for the 20th pair of folders.

$Results20 = Compare-Object $FilesInFolderX20 $FilesInFolderY20
-Property Name, LastWriteTime -PassThru | Select-Object FullName

Solution

  • Here is a way you can handle that with Get-Variable CmdLet

    $a1 = 12,13 
    $a2 = 13,14
    
    $b = 1
    Compare-Object   (Get-Variable "a$b").Value (Get-Variable -Name "a$($b+1)").Value
    

    In your case you can use :

    Compare-Object (Get-Variable "$FilesInFolderX$F").value (Get-Variable "$FilesInFolderY$F").value