param(
    [Parameter(Mandatory=$true)]
    [string]$Path1,

    [Parameter(Mandatory=$true)]
    [string]$Path2
)

# WinMergeのパスを指定
$winmergePath = "C:\\Program Files (x86)\\WinMerge\\WinMergeU.exe"

# WinMergeを実行
& $winmergePath -e -ub -dl "Left" -dr "Right" $Path1 $Path2

このスクリプトは、2つの必須パラメータ($Path1 および $Path2)を受け取ります。これらは比較する2つのファイルまたはディレクトリのパスです。

スクリプトは次に、WinMergeの実行可能ファイルへのパスを指定します。このパスはあなたのシステムによって異なる可能性があるため、必要に応じて調整してください。

最後に、スクリプトはWinMergeを起動し、$Path1$Path2を比較します。-eオプションはWinMergeが終了するまでスクリプトの実行を停止します。-ubオプションはバックアップファイルを作成しないことを指定します。-dl "Left"-dr "Right"オプションは、比較ウィンドウの左側と右側のパネルに表示されるタイトルを設定します。

このスクリプトを.ps1ファイルに保存し、PowerShellから実行することができます。例えば、スクリプトをcompare-files.ps1という名前で保存し、C:\\path\\to\\file1C:\\path\\to\\file2を比較する場合、次のコマンドを実行します:

.\\compare-files.ps1 -Path1 "C:\\path\\to\\file1" -Path2 "C:\\path\\to\\file2"
`

了解しました。その場合、スクリプトは次のようになります:

param(
    [Parameter(Mandatory=$true)]
    [string]$Path1,

    [Parameter(Mandatory=$true)]
    [string]$Path2
)

# WinMergeのパスを指定
$winmergePath64 = "C:\\Program Files\\WinMerge\\WinMergeU.exe"
$winmergePath32 = "C:\\Program Files (x86)\\WinMerge\\WinMergeU.exe"

# 存在するWinMergeのパスを選択
$winmergePath = $null
if (Test-Path $winmergePath64) {
    $winmergePath = $winmergePath64
} elseif (Test-Path $winmergePath32) {
    $winmergePath = $winmergePath32
}

if ($winmergePath -eq $null) {
    Write-Output "WinMerge not found"
    exit 1
}

# WinMergeを実行
& $winmergePath -e -ub -dl "Left" -dr "Right" $Path1 $Path2

このスクリプトは、最初に64ビット版のWinMergeがインストールされているか確認します。64ビット版が存在しない場合は、32ビット版が存在するか確認します。どちらも存在しない場合、スクリプトはエラーメッセージを出力して終了します。

このスクリプトを.ps1ファイルに保存し、PowerShellから実行することができます。例えば、スクリプトをcompare-files.ps1という名前で保存し、C:\\path\\to\\file1C:\\path\\to\\file2を比較する場合、次のコマンドを実行します:

.\\compare-files.ps1 -Path1 "C:\\path\\to\\file1" -Path2 "C:\\path\\to\\file2"
`

了解しました。以下のように修正したスクリプトを提供します:

param(
    [Parameter(Mandatory=$true)]
    [string]$Path1,

    [Parameter(Mandatory=$true)]
    [string]$Path2
)

# WinMergeのパスを辞書で指定
$winmergePaths = @{
    "64bit" = "C:\\Program Files\\WinMerge\\WinMergeU.exe"
    "32bit" = "C:\\Program Files (x86)\\WinMerge\\WinMergeU.exe"
}

# 存在するWinMergeのパスを選択
$winmergePath = $null
foreach ($path in $winmergePaths.Values) {
    if (Test-Path $path) {
        $winmergePath = $path
        break
    }
}

if ($winmergePath -eq $null) {
    Write-Output "WinMerge not found"
    exit 1
}

# WinMergeを実行
& $winmergePath -e -ub -dl "Left" -dr "Right" $Path1 $Path2

このスクリプトは、WinMergeのパスを辞書で指定し、foreachループを使用して各パスが存在するかどうかを確認します。存在するパスが見つかった場合、そのパスがWinMergeの実行パスとして選択され、ループは即座に終了します。どのパスも存在しない場合、スクリプトはエラーメッセージを出力して終了します。

了解しました。PowerShellのパイプライン機能を活用すると、スクリプトは以下のようになります:

param(
    [Parameter(Mandatory=$true)]
    [string]$Path1,

    [Parameter(Mandatory=$true)]
    [string]$Path2
)

# WinMergeのパスを辞書で指定
$winmergePaths = @{
    "64bit" = "C:\\Program Files\\WinMerge\\WinMergeU.exe"
    "32bit" = "C:\\Program Files (x86)\\WinMerge\\WinMergeU.exe"
}

# 存在するWinMergeのパスを選択
$winmergePath = $winmergePaths.Values | Where-Object { Test-Path $_ } | Select-Object -First 1

if ($winmergePath -eq $null) {
    Write-Output "WinMerge not found"
    exit 1
}

# WinMergeを実行
& $winmergePath -e -ub -dl "Left" -dr "Right" $Path1 $Path2

このスクリプトは、Where-Objectコマンドレットを使ってパスが存在するかをフィルタリングし、Select-Object -First 1で最初に見つかったパスを選択しています。これにより、foreachループを使わずに同じ結果を得ることができます。

この.ps1ファイルをコマンドプロンプトから実行するには、以下の手順に従います。

  1. PowerShellスクリプトを有効にする: これは、PowerShellスクリプトがシステム上で実行できるようにするための一時的な設定です。次のコマンドを実行します(管理者としてコマンドプロンプトを起動する必要があります):

cmdCopy codepowershell -Command "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser"

  1. スクリプトを実行する: その後、次のように.ps1スクリプトを実行します:cmdCopy codepowershell -File "C:\\path\\to\\your\\script.ps1" -Path1 "C:\\path\\to\\file1" -Path2 "C:\\path\\to\\file2"
    ここで、C:\\path\\to\\your\\script.ps1はスクリプトファイルのパスであり、C:\\path\\to\\file1C:\\path\\to\\file2は比較するファイルまたはディレクトリのパスです。これらを適切なパスに置き換えてください。
  2. 実行ポリシーをリセットする(オプション): スクリプトの実行が終わったら、PowerShellの実行ポリシーを元の設定に戻すことが推奨されます。次のコマンドを実行します(管理者としてコマンドプロンプトを起動する必要があります):cmdCopy code`powershell -Command "Set-ExecutionPolicy -ExecutionPolicy Restricted -Scope CurrentUser"