How to Compare Last two modified files using powershell compare-object





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















i have a powershell script to compare two files as per user selection, but i want to compare last two modified files without asking user input



    Set-ExecutionPolicy Unrestricted -force
cd
cd .script
Write-Host " "
Write-Host "Available Files "
Write-Host "================="
Get-ChildItem | Format-table -Property Name -HideTableHeaders
$file = 'c:scriptmismatchfound.txt'
$ref = get-content (Read-Host "Enter Reference FileName")
$dif = get-content (Read-Host "Enter Difference FileName")
Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200









share|improve this question





























    1















    i have a powershell script to compare two files as per user selection, but i want to compare last two modified files without asking user input



        Set-ExecutionPolicy Unrestricted -force
    cd
    cd .script
    Write-Host " "
    Write-Host "Available Files "
    Write-Host "================="
    Get-ChildItem | Format-table -Property Name -HideTableHeaders
    $file = 'c:scriptmismatchfound.txt'
    $ref = get-content (Read-Host "Enter Reference FileName")
    $dif = get-content (Read-Host "Enter Difference FileName")
    Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200









    share|improve this question

























      1












      1








      1


      1






      i have a powershell script to compare two files as per user selection, but i want to compare last two modified files without asking user input



          Set-ExecutionPolicy Unrestricted -force
      cd
      cd .script
      Write-Host " "
      Write-Host "Available Files "
      Write-Host "================="
      Get-ChildItem | Format-table -Property Name -HideTableHeaders
      $file = 'c:scriptmismatchfound.txt'
      $ref = get-content (Read-Host "Enter Reference FileName")
      $dif = get-content (Read-Host "Enter Difference FileName")
      Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200









      share|improve this question














      i have a powershell script to compare two files as per user selection, but i want to compare last two modified files without asking user input



          Set-ExecutionPolicy Unrestricted -force
      cd
      cd .script
      Write-Host " "
      Write-Host "Available Files "
      Write-Host "================="
      Get-ChildItem | Format-table -Property Name -HideTableHeaders
      $file = 'c:scriptmismatchfound.txt'
      $ref = get-content (Read-Host "Enter Reference FileName")
      $dif = get-content (Read-Host "Enter Difference FileName")
      Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200






      powershell






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 13:11









      karhtikkarhtik

      12210




      12210
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Instead of write-Error you can use write-host, this will work in all version of powershell



          cd
          cd .Scripts
          $File = 'c:scriptmismatchfound.txt'
          $CompareFiles = Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 2
          If ($Files.count -eq 2) {
          $Ref = get-content ($CompareFiles[0])
          $Dif = get-content ($CompareFiles[1])

          Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200
          } Else {
          Write-Host "File missing " -ForegroundColor White -BackgroundColor Red
          }





          share|improve this answer
























          • This one works fine, thank you for the time and answer.

            – karhtik
            Nov 23 '18 at 14:47



















          2














          You can do this as follows:



          $FilePath = '.pathtoyourfiles'
          $File = 'c:scriptmismatchfound.txt'
          $CompareFiles = Get-ChildItem $FilePath -File | Sort-Object LastWriteTime | Select-Object -Last 2

          If ($Files.count -eq 2) {
          $Ref = get-content $CompareFiles[0]
          $Dif = get-content $CompareFiles[1]

          Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200
          }
          Else{
          Write-Error '2 files not returned.'
          }


          This uses Get-ChildItem to return only files from the path you specify in $FilePath. It then uses Sort-Object to sort them by the LastWriteTime attribute of the files. Then it uses Select-Object to filter for just the last two objects returned in that collection.



          Then we use a if to validate that we have two files and if we do, then put them in the $Ref and $Dif variables and use Compare-Object per your code.



          If two files aren't returned, we return an error.






          share|improve this answer


























          • thanks for the time, the above script displays error, i think we have to add this $Ref=get-content ($CompareFiles[0]) , i edited your answer and added the same

            – karhtik
            Nov 23 '18 at 14:38













          • Yes you’re right.

            – Mark Wragg
            Nov 23 '18 at 15:14












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447376%2fhow-to-compare-last-two-modified-files-using-powershell-compare-object%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Instead of write-Error you can use write-host, this will work in all version of powershell



          cd
          cd .Scripts
          $File = 'c:scriptmismatchfound.txt'
          $CompareFiles = Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 2
          If ($Files.count -eq 2) {
          $Ref = get-content ($CompareFiles[0])
          $Dif = get-content ($CompareFiles[1])

          Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200
          } Else {
          Write-Host "File missing " -ForegroundColor White -BackgroundColor Red
          }





          share|improve this answer
























          • This one works fine, thank you for the time and answer.

            – karhtik
            Nov 23 '18 at 14:47
















          0














          Instead of write-Error you can use write-host, this will work in all version of powershell



          cd
          cd .Scripts
          $File = 'c:scriptmismatchfound.txt'
          $CompareFiles = Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 2
          If ($Files.count -eq 2) {
          $Ref = get-content ($CompareFiles[0])
          $Dif = get-content ($CompareFiles[1])

          Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200
          } Else {
          Write-Host "File missing " -ForegroundColor White -BackgroundColor Red
          }





          share|improve this answer
























          • This one works fine, thank you for the time and answer.

            – karhtik
            Nov 23 '18 at 14:47














          0












          0








          0







          Instead of write-Error you can use write-host, this will work in all version of powershell



          cd
          cd .Scripts
          $File = 'c:scriptmismatchfound.txt'
          $CompareFiles = Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 2
          If ($Files.count -eq 2) {
          $Ref = get-content ($CompareFiles[0])
          $Dif = get-content ($CompareFiles[1])

          Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200
          } Else {
          Write-Host "File missing " -ForegroundColor White -BackgroundColor Red
          }





          share|improve this answer













          Instead of write-Error you can use write-host, this will work in all version of powershell



          cd
          cd .Scripts
          $File = 'c:scriptmismatchfound.txt'
          $CompareFiles = Get-ChildItem -File | Sort-Object LastWriteTime | Select-Object -Last 2
          If ($Files.count -eq 2) {
          $Ref = get-content ($CompareFiles[0])
          $Dif = get-content ($CompareFiles[1])

          Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200
          } Else {
          Write-Host "File missing " -ForegroundColor White -BackgroundColor Red
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 14:45









          T.AnandT.Anand

          10029




          10029













          • This one works fine, thank you for the time and answer.

            – karhtik
            Nov 23 '18 at 14:47



















          • This one works fine, thank you for the time and answer.

            – karhtik
            Nov 23 '18 at 14:47

















          This one works fine, thank you for the time and answer.

          – karhtik
          Nov 23 '18 at 14:47





          This one works fine, thank you for the time and answer.

          – karhtik
          Nov 23 '18 at 14:47













          2














          You can do this as follows:



          $FilePath = '.pathtoyourfiles'
          $File = 'c:scriptmismatchfound.txt'
          $CompareFiles = Get-ChildItem $FilePath -File | Sort-Object LastWriteTime | Select-Object -Last 2

          If ($Files.count -eq 2) {
          $Ref = get-content $CompareFiles[0]
          $Dif = get-content $CompareFiles[1]

          Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200
          }
          Else{
          Write-Error '2 files not returned.'
          }


          This uses Get-ChildItem to return only files from the path you specify in $FilePath. It then uses Sort-Object to sort them by the LastWriteTime attribute of the files. Then it uses Select-Object to filter for just the last two objects returned in that collection.



          Then we use a if to validate that we have two files and if we do, then put them in the $Ref and $Dif variables and use Compare-Object per your code.



          If two files aren't returned, we return an error.






          share|improve this answer


























          • thanks for the time, the above script displays error, i think we have to add this $Ref=get-content ($CompareFiles[0]) , i edited your answer and added the same

            – karhtik
            Nov 23 '18 at 14:38













          • Yes you’re right.

            – Mark Wragg
            Nov 23 '18 at 15:14
















          2














          You can do this as follows:



          $FilePath = '.pathtoyourfiles'
          $File = 'c:scriptmismatchfound.txt'
          $CompareFiles = Get-ChildItem $FilePath -File | Sort-Object LastWriteTime | Select-Object -Last 2

          If ($Files.count -eq 2) {
          $Ref = get-content $CompareFiles[0]
          $Dif = get-content $CompareFiles[1]

          Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200
          }
          Else{
          Write-Error '2 files not returned.'
          }


          This uses Get-ChildItem to return only files from the path you specify in $FilePath. It then uses Sort-Object to sort them by the LastWriteTime attribute of the files. Then it uses Select-Object to filter for just the last two objects returned in that collection.



          Then we use a if to validate that we have two files and if we do, then put them in the $Ref and $Dif variables and use Compare-Object per your code.



          If two files aren't returned, we return an error.






          share|improve this answer


























          • thanks for the time, the above script displays error, i think we have to add this $Ref=get-content ($CompareFiles[0]) , i edited your answer and added the same

            – karhtik
            Nov 23 '18 at 14:38













          • Yes you’re right.

            – Mark Wragg
            Nov 23 '18 at 15:14














          2












          2








          2







          You can do this as follows:



          $FilePath = '.pathtoyourfiles'
          $File = 'c:scriptmismatchfound.txt'
          $CompareFiles = Get-ChildItem $FilePath -File | Sort-Object LastWriteTime | Select-Object -Last 2

          If ($Files.count -eq 2) {
          $Ref = get-content $CompareFiles[0]
          $Dif = get-content $CompareFiles[1]

          Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200
          }
          Else{
          Write-Error '2 files not returned.'
          }


          This uses Get-ChildItem to return only files from the path you specify in $FilePath. It then uses Sort-Object to sort them by the LastWriteTime attribute of the files. Then it uses Select-Object to filter for just the last two objects returned in that collection.



          Then we use a if to validate that we have two files and if we do, then put them in the $Ref and $Dif variables and use Compare-Object per your code.



          If two files aren't returned, we return an error.






          share|improve this answer















          You can do this as follows:



          $FilePath = '.pathtoyourfiles'
          $File = 'c:scriptmismatchfound.txt'
          $CompareFiles = Get-ChildItem $FilePath -File | Sort-Object LastWriteTime | Select-Object -Last 2

          If ($Files.count -eq 2) {
          $Ref = get-content $CompareFiles[0]
          $Dif = get-content $CompareFiles[1]

          Compare-Object -ReferenceObject $ref -DifferenceObject $dif -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200
          }
          Else{
          Write-Error '2 files not returned.'
          }


          This uses Get-ChildItem to return only files from the path you specify in $FilePath. It then uses Sort-Object to sort them by the LastWriteTime attribute of the files. Then it uses Select-Object to filter for just the last two objects returned in that collection.



          Then we use a if to validate that we have two files and if we do, then put them in the $Ref and $Dif variables and use Compare-Object per your code.



          If two files aren't returned, we return an error.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 15:14

























          answered Nov 23 '18 at 13:30









          Mark WraggMark Wragg

          15k42146




          15k42146













          • thanks for the time, the above script displays error, i think we have to add this $Ref=get-content ($CompareFiles[0]) , i edited your answer and added the same

            – karhtik
            Nov 23 '18 at 14:38













          • Yes you’re right.

            – Mark Wragg
            Nov 23 '18 at 15:14



















          • thanks for the time, the above script displays error, i think we have to add this $Ref=get-content ($CompareFiles[0]) , i edited your answer and added the same

            – karhtik
            Nov 23 '18 at 14:38













          • Yes you’re right.

            – Mark Wragg
            Nov 23 '18 at 15:14

















          thanks for the time, the above script displays error, i think we have to add this $Ref=get-content ($CompareFiles[0]) , i edited your answer and added the same

          – karhtik
          Nov 23 '18 at 14:38







          thanks for the time, the above script displays error, i think we have to add this $Ref=get-content ($CompareFiles[0]) , i edited your answer and added the same

          – karhtik
          Nov 23 '18 at 14:38















          Yes you’re right.

          – Mark Wragg
          Nov 23 '18 at 15:14





          Yes you’re right.

          – Mark Wragg
          Nov 23 '18 at 15:14


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447376%2fhow-to-compare-last-two-modified-files-using-powershell-compare-object%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy