- PowerShell 教程
- PowerShell - 主頁
- PowerShell - 概述
- PowerShell - 環境設定
- PowerShell - Cmdlet
- PowerShell - 檔案與資料夾
- PowerShell - 日期和時間
- PowerShell - 檔案 I/O
- PowerShell - 高階 Cmdlet
- PowerShell - 指令碼
- PowerShell - 特殊變數
- PowerShell - 運算子
- PowerShell - 迴圈
- PowerShell - 條件
- PowerShell - 陣列
- PowerShell - 雜湊表
- PowerShell - Regex
- PowerShell - 反引號
- PowerShell - 括號
- PowerShell - 別名
- PowerShell 有用的資源
- PowerShell - 快速指南
- PowerShell - 有用資源
- PowerShell - 討論
PowerShell - Compare-Object Cmdlet
Compare-Object cmdlet 可用於比較兩個物件。
在這些示例中,我們將看到 Compare-Object cmdlet 的實際運用。
示例 1
在本示例中,我們首先在 D:\temp\test 中有一個名為 test.txt 的檔案,其內容為“Welcome to TutorialsPoint.Com”,還有一個名為 test1.txt 的檔案,其內容為“Hello World!”和“Welcome to TutorialsPoint.Com”,分兩行顯示。
比較這些檔案。在 PowerShell ISE 控制檯中鍵入以下命令。將顯示公共行。
Compare-Object -ReferenceObject $(Get-Content D:\temp\test\test.txt) -DifferenceObject $(Get-Content D:\temp\test\test1.txt)
輸出
可以在 PowerShell 控制檯中看到以下輸出。
InputObject SideIndicator ----------- ------------- Hello World! =>
示例 2
比較檔案的內容。在 PowerShell ISE 控制檯中鍵入以下命令。將顯示帶有指示器的所有行。
在 PowerShell ISE 控制檯中鍵入以下命令
Compare-Object -ReferenceObject $(Get-Content D:\temp\test\test.txt) -DifferenceObject $(Get-Content D:\temp\test\test1.txt) -IncludeEqual
輸出
可以在 PowerShell 控制檯中看到以下輸出。
InputObject SideIndicator ----------- ------------- Welcome to TutorialsPoint.Com == Hello World! =>
powershell_advanced_cmdlets.htm
廣告