HOWTO · PowerShell

如何使用 PowerShell 檢查文件是否包含特定字符串

您可以使用 Windows PowerShell 檢查文本檔案是否包含特定字符串。

在處理 Windows PowerShell 時,在文本文件中搜尋特定字串是一項常見任務。雖然 Unix 和 Linux 環境擁有可靠的 grep 命令,PowerShell 提供了一種替代的方法,使用 Select-StringGet-ContentGet-ChildItem cmdlet。

在本文中,我們將探討如何使用 PowerShell 在文本文件中尋找字串的各種方法。

使用 PowerShell 的 Select-String 檢查文件是否包含特定字串

假設我們的機器上有一個名為 Demo.txt 的文件,我們想在其中尋找字串 Demonstration

這是實現該操作的 PowerShell 腳本:

Select-String -Path C:\Users\pc\Demo\Demo.txt -Pattern 'Demonstration'

在這個腳本中,我們使用 Select-String 配合 -Path 參數來指定文件位置,以及 -Pattern 參數來指定要查找的字串。

如果我們的文件包含指定的模式('Demonstration'),控制台將輸出文件名、行號和包含該字串的行。

輸出:

C:\Users\pc\Demo\Demo.txt:1:This purely for demonstration

上述輸出顯示了 Demo.txt 文件的完整路徑、包含我們字串的行(第 1 行)和該行的內容(This is purely for demonstration)。

但是,如果我們只是想知道文件是否包含或不包含該字串呢?在這種情況下,我們可以將腳本修改如下:

$Knw = Select-String -Path C:\Users\pc\Demo\Demo.txt -Pattern "Demonstration"

if ($Knw -ne $null) {
    echo Contains String
}
else {
    echo Does not Contain String
}

在這個腳本中,我們定義了一個變數 $Knw,該變數包含使用 Select-String 在指定路徑的文件中搜尋字串 "Demonstration" 的結果。然後,我們檢查變數 $Knw 是否不等於 $null,這意味著在文件中找到了一個匹配項。

如果找到匹配項,則輸出 "Contains String."。如果沒有找到匹配項,則輸出 "Does not Contain String."

輸出:

Contains String

由於我們的文件確實包含模式 "Demonstration",所以輸出顯示 Contains String

使用 PowerShell 的 Get-Content 檢查文件是否包含特定字串

Get-Content 是一個 cmdlet,用於獲取指定路徑下文件的內容,並讀取該文件的內容以返回字串對象。要在我們的 Demo.txt 文件中使用 Get-Content,我們將使用下面所示的腳本:

Get-Content -Path C:\Users\pc\Demo\Demo.txt | Select-String -Pattern "Demonstration"

在這個腳本中,我們使用 Get-Content 來讀取由 -Path 參數指定的 Demo.txt 文件的內容,並將其轉發給 Select-String 命令,後者使用 -Pattern 參數來定位 Demonstration 字串。

輸出:

This purely for demonstration

如果我們只想確認該字串的存在,我們可以使用下面顯示的腳本:

If (Get-Content C:\Users\pc\Demo\Demo.txt | % { $_ -match "Demonstration" }) {
    echo Contains String
}
else {
    echo Does not Contains String
}

在這個腳本中,我們使用 Get-Content 來閱讀位於 C:\Users\pc\Demo\Demo.txt 的文件數據,並使用管道(|)逐行處理文件。在 %{} 區塊內,我們檢查每一行是否包含字串 "Demonstration",使用 -match 運算符。

如果文件中的任何行包含 "Demonstration",則輸出 "Contains String"。如果文件中的所有行都不包含 "Demonstration",則輸出 "Does not Contains String"

輸出:

Contains String

由於我們的文件包含指定的字串("Demonstration"),因此輸出結果為 Contains String

檢查多個包含特定字串的文件

如果你有超過五十個文本文件,並且想要在所有文件中搜尋一個字串呢?我們將討論兩種方法來做到這一點。

在 PowerShell 中使用 Get-ChildItemSearch-String

我們可以使用 Get-ChildItemSelect-String cmdlet。Get-ChildItem cmdlet 將根據我們的搜尋條件找到一個或多個文件。

讓我們看一個例子。假設我們的 Demo 文件夾有超過五十個文本文件,我們可以使用這個腳本在所有文件中搜尋字串 Demonstration

Get-ChildItem -Path C:\Users\pc\Demo\ -Recurse | Select-String -Pattern 'Demonstration'

在這個腳本中,我們使用 Get-ChildItem 列出指定目錄(-Path)及其子目錄(-Recurse)中的文件。管道運算符(|)將文件列表傳遞給 Select-String cmdlet。

最後,我們使用 Select-String 在每個文件中搜尋特定模式 'Demonstration'

控制台將輸出包含我們字串的文件名、行號和包含該字串的行。

輸出:

C:\Users\pc\Demo\Demo.txt:1:This purely for demonstration
C:\Users\pc\Demo\Insert.txt:1:We will use this demonstration for a test trial
C:\Users\pc\Demo\myfile.txt:1:This is a demonstration file for Get-ChildItem

在 PowerShell 中使用 Select-String

我們還可以僅使用 Select-String 來檢查多個文件中的特定字串。

Select-String -Path C:\Users\pc\Demo\*.txt -Pattern 'Demonstration'

在這個腳本中,我們在 -Path 參數中使用通配符(*)來搜尋 Demo 文件夾中的所有文本文件。Select-String cmdlet 將在所有匹配的文件中搜尋該字串。

輸出:

C:\Users\pc\Demo\Demo.txt:1:This purely for demonstration
C:\Users\pc\Demo\Insert.txt:1:We will use this demonstration for a test trial
C:\Users\pc\Demo\myfile.txt:1:This is a demonstration file for Get-ChildItem

與上面相同,控制台將輸出包含我們字串的文件名、行號和包含該字串的行。

結論

Windows PowerShell 提供了一組工具,用於有效搜索文本文件中的字串。無論你是更喜歡使用 Select-StringGet-Content,還是結合使用 Get-ChildItemSelect-String,你都可以輕鬆找到並操作文件中的數據。

使用這些 cmdlet,你將不會在 PowerShell 中尋找信息時遇到困難,使文本文件的搜索變得輕而易舉。