このエントリーをはてなブックマークに追加


PowerShellでdiffを取る・Compare-Object

PowerShellのdiffはCompare-Objectコマンドレットになります。
実際にCompare-Objectを使った使用例を以下に記します。


・ファイルの比較をしてみる 比較の構文は以下のようになります。

Compare-Object (Get-Content ファイル) (Get-Content ファイル)

以下のテキストファイルを2つ用意し、Compare-Objectコマンドレットで比較した時の出力です。

  • a.txt
    Windows 3.1
    Windows 95
    Windows 2000
    Windows Me
    Windows XP
    Windows Vista
    windows 7
    Windows 8
    Windows 8.1
    Windows 10
  • b.txt
    Windows 3.1
    Windows 95
    Windows 98
    Windows 2000
    Windows Me
    Windows XP
    Windows Vista
    Windows 7
    Windows 10

実行結果1(大文字小文字区別なし)

比較結果が以下のように表示されます。
大文字小文字が区別されていないのがわかります。(windows 7とWindows 7が同じになっている)

PS C:\Users\sakura> Compare-Object (Get-Content a.txt) (Get-Content b.txt)

InputObject                                                 SideIndicator
-----------                                                 -------------
Windows 98                                                  =>
Windows 8                                                   <=
Windows 8.1                                                 <=

実行結果2(大文字小文字の区別あり)

オブションで-CaseSensitiveを追加すれば大文字小文字が区別されます。

PS C:\Users\sakura> Compare-Object -CaseSensitive (Get-Content a.txt) (Get-Content b.txt)

InputObject                                                 SideIndicator
-----------                                                 -------------
Windows 98                                                  =>
Windows 7                                                   =>
windows 7                                                   <=
Windows 8                                                   <=
Windows 8.1                                                 <=

以上、Compare-Objectコマンドレットを使ったファイル比較方法でした。



トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2015-05-28 (木) 21:25:25