powershellcmdfile-comparison

PowerShell: How to get cmd comp to run quietly?


I want to test the results of both the cmd commands comp and FC running in PowerShell on a series of files. I have succeeded in getting FC to run quietly in a script and yield correct results. In a cmd batch file, comp can run quietly by prefixing it with echo N |, but that is not working in PowerShell.

Here is a test script:

$LastExitCode = 99
$sItem_1 = "C:\temp\file 1.txt"
$sItem_2 = "C:\temp\file 2.txt"
write-output "Do FC:"
cmd.exe /c "FC /b ""$sItem_1"" ""$sItem_2"" > null"
write-output $LastExitCode
$LastExitCode = 99
write-output "Do comp:"
cmd.exe /c "echo N | comp ""$sItem_1"" ""$sItem_2"" > null"
write-output $LastExitCode

and here is the console output of that script:

PS> test 2.ps1
Do FC:
0
Do comp:
cmd.exe : Compare more files (Y/N) ? 
At test 2.ps1:9 char:1
+ cmd.exe /c "echo N | comp ""$sItem_1"" ""$sItem_2"" > null"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Compare more files (Y/N) ? :String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PS> 

In the second line of output, the zero exit code means that FC found the files to be identical. But when PowerShell attempted to run comp, it choked on the "Compare more" prompt.

How can I get this to run?


Solution

  • Therefore:

    $sItem_1 = "C:\temp\file 1.txt"
    $sItem_2 = "C:\temp\file 2.txt"
    
    "Do FC:"
    fc /b$ sItem_1 $sItem_2 >$null
    $LastExitCode
    
    $LastExitCode = 99
    "Do comp:"
    comp /M $sItem_1 $sItem_2 >$null
    $LastExitCode