How to Compare Last two modified files using powershell compare-object
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
add a comment |
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
add a comment |
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
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
powershell
asked Nov 23 '18 at 13:11
karhtikkarhtik
11710
11710
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
}
This one works fine, thank you for the time and answer.
– karhtik
Nov 23 '18 at 14:47
add a comment |
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
}
This one works fine, thank you for the time and answer.
– karhtik
Nov 23 '18 at 14:47
add a comment |
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
}
This one works fine, thank you for the time and answer.
– karhtik
Nov 23 '18 at 14:47
add a comment |
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
}
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
}
answered Nov 23 '18 at 14:45
T.AnandT.Anand
10019
10019
This one works fine, thank you for the time and answer.
– karhtik
Nov 23 '18 at 14:47
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
edited Nov 23 '18 at 15:14
answered Nov 23 '18 at 13:30
Mark WraggMark Wragg
14.2k42143
14.2k42143
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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