PowerShell を使用してファイルに特定の文字列が含まれているかを確認する方法
-
PowerShell で
Select-String
を使用してファイルに特定の文字列が含まれているかを確認する -
PowerShell で
Get-Content
を使用してファイルに特定の文字列が含まれているかを確認する - 特定の文字列が含まれている複数のファイルを確認する
- 結論

特定の文字列をテキストファイル内で検索することは、Windows PowerShell を使用する際の一般的なタスクです。Unix や Linux 環境では信頼できる grep
コマンドがありますが、PowerShell は Select-String
、Get-Content
、および Get-ChildItem
コマンドレットを使用して代替アプローチを提供します。
この記事では、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
は、指定されたパスのファイルの内容を取得し、ファイルの内容を読み取って文字列オブジェクトを返すコマンドレットです。私たちの 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
にあるファイルのデータを読み取り、パイプ(|
)を使ってファイルの各行を 1 行ずつ処理します。%{}
ブロック内では、各行に"Demonstration"
という文字列が含まれているかを -match
演算子を使用して確認します。
ファイル内のいずれかの行に"Demonstration"
が含まれている場合、"Contains String"
を表示します。ファイル内のいずれの行にも"Demonstration"
が含まれていない場合、"Does not Contains String"
を表示します。
出力:
Contains String
私たちのファイルには指定された文字列("Demonstration"
)が含まれているため、出力は Contains String
となります。
特定の文字列が含まれている複数のファイルを確認する
50 以上のテキストファイルがあり、それらすべての中で文字列を検索したい場合はどうしますか?これを行う 2つの方法について議論します。
PowerShell で Get-ChildItem
と Search-String
を使用する
Get-ChildItem
と Select-String
コマンドレットを使用できます。Get-ChildItem
コマンドレットは、検索条件に基づいて 1つ以上のファイルを見つけ出します。
例を見てみましょう。私たちの Demo
フォルダに 50 以上のテキストファイルがあると仮定し、すべてのファイルで文字列 Demonstration
を検索するために次のスクリプトを使用できます:
Get-ChildItem -Path C:\Users\pc\Demo\ -Recurse | Select-String -Pattern 'Demonstration'
このスクリプトでは、Get-ChildItem
を使用して、指定されたディレクトリ(-Path
)とそのサブディレクトリ内のファイルをリストします。パイプオペレーター(|
)がファイルのリストを Select-String
コマンドレットに渡します。
最後に、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
コマンドレットは、すべての一致ファイル内の文字列を検索します。
出力:
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-String
、Get-Content
、または Get-ChildItem
と Select-String
の組み合わせを使用するかに関わらず、ファイル内のデータを簡単に見つけて操作できます。
これらのコマンドレットを使用すれば、必要な情報を見つけるのも容易になり、PowerShell でのテキストファイル検索が手軽になります。
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn