title: PowerShell: Compare files in two different folder
body: <p>I have a source folder and a destination folder. If a file in the source folder is newer than the file in the destination folder, it must be replaced.
  I have tried it myself, but it does not work as it should. The code is probably solved too complicated for professionals.</p>
  <pre><code>$SourceAlarmgroups = $rootPath + &quot;\..\..\..\..\..\..\Logical\VCShared\AlarmGroups&quot;
  $DestinationAlarmgroups = &quot;$rootPath\Alarmgroups&quot;
  
  $lastModifiedDateSource = (Get-Item $SourceAlarmgroups).LastWriteTime
  $lastModifiedDateDestination = (Get-Item $DestinationAlarmgroups).LastWriteTime
  
  $FolderSource = Get-ChildItem -Recurse -Path $SourceAlarmgroups
  $FolderDestination = Get-ChildItem -Recurse -Path $DestinationAlarmgroups
  
  $FolderCheck = Compare-Object -ReferenceObject $FolderSource -DifferenceObject $FolderDestination
  
  forEach($file in $FolderCheck){
      if ($file.sideindicator -eq &quot;&lt;=&quot;){
          $SetWriteCmd = 1
      }
  }
  if ($lastModifiedDateSource -gt $lastModifiedDateDestination -or $SetWriteCmd -eq 1 ){
      Write-Host &quot; Collect alarm files...&quot; 
      Get-ChildItem $SourceAlarmgroups -Include *.algrp | Copy-Item -Destination $DestinationAlarmGroups -Force -PassThru
      &amp;$Script2RunAlarmexport
  }else{ 
      Write-Host &quot;Alarms: Already up to date&quot;   
  }
  </code></pre>
  <p>Two problems arise:</p>
  <ol>
  <li>referencing to an empty folder does not work, i.e. if the destination folder is empty there is an error.</li>
  <li>the files are not copied from source to destination</li>
  </ol>
  <p>Can someone help me.
  TIA!</p>