Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
function Invoke-WinMerge {
Wrapper for WinMerge.
PowerShell wrapper for WinMerge. Let's you launch file/folder comparison using WinMerge
from the PowerShell console.
Invoke-WinMerge c:\temp\file1.txt c:\temp\file2.txt
Will launch WinMerge to compare file1.txt and file2.txt.
Invoke-WinMerge c:\folder1 c:\folder2
Will launch WinMerge to compare folder1 and folder2.
Author: Øyvind Kallstad
Date: 17.01.2016
Version: 1.1
param (
# Specifies the folder, file, or project file to open on the left side.
[Parameter(Position = 1, Mandatory = $true)]
[string] $LeftPath,
# Specifies the folder, file, or project file to open on the right side.
[Parameter(Position = 2, Mandatory = $true)]
[string] $RightPath,
# Path to WinMergeU.exe. If not specified, the function will try to locate
# the path from the Windows Registry.
[string] $PathToWinMergeU = $null,
# Applies a specified filter to restrict the comparison.
# The filter can be a filemask like *.h *.cpp, or the name of a file filter like XML/HTML Devel.
# Add quotation marks around a filter mask or name that contains spaces.
[string] $Filter,
# Compares all files in all subfolders (recursive compare).
# Unique folders (occurring only on one side) are listed in the compare result as separate items.
# Note that including subfolders can increase compare time significantly.
# Without this parameter, WinMerge lists only files and subfolders at the top level of the two target folders. It does not compare the subfolders.
[switch] $Recurse,
# Closes WinMerge when you start a comparison of identical files. It will not work when comparing folders.
# The parameter has no effect after the comparison, for example if the files become identical as a result of merging or editing.
[switch] $CloseOnSame
$regPath1 = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinMergeU.exe'
$regPath2 = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinMergeU.exe'
$notFoundMessage = 'WinMergeU.exe not found! You can download WinMerge from'
# if no path to WinMergeU.exe is given by the user, try to get it from the registry
if (-not $PathToWinMergeU) {
if (Test-Path -Path $regPath1) {
$exePath = Get-ItemProperty $regPath1 | Select-Object -ExpandProperty '(default)'
elseif (Test-Path -Path $regPath2) {
$exePath = Get-ItemProperty $regPath2 | Select-Object -ExpandProperty '(default)'
else {
Write-Warning $notFoundMessage
else {
$exePath = $PathToWinMergeU
if (Test-Path -Path $exePath) {
$leftExist = Test-Path -Path $LeftPath
$rightExist = Test-Path -Path $RightPath
if ($leftExist -and $rightExist) {
if ((Get-Item -Path $LeftPath).FullName -eq (Get-Item -Path $RightPath).FullName) {
Write-Warning 'Left and Right paths are the same. Did you mean to do that?'
$parameters = New-Object System.Collections.ArrayList
# /e enables you to close WinMerge with a single Esc key press.
# This is useful when you use WinMerge as an external compare application: you can close WinMerge quickly, like a dialog.
# Without this parameter, you might have to press Esc multiple times to close all its windows.
# /u prevents WinMerge from adding either path (left or right) to the Most Recently Used (MRU) list.
# External applications should not add paths to the MRU list in the Select Files or Folders dialog.
if ($CloseOnSame) {
if ($Recurse) {
if ($Filter) {
Start-Process -FilePath $exePath -ArgumentList $parameters
else {
if (-not $leftExist) {
Write-Warning "$($LeftPath) not found!"
if (-not $rightExist) {
Write-Warning "$($RightPath) not found!"
else {
Write-Warning $notFoundMessage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment