もっと詳しく

を使用して PowerShell の置換 () メソッドまたは PowerShell 交換 演算子を使用すると、ファイル内の行を簡単に置き換えることができます。 を使用して、ファイル内の文字列を置き換え、更新されたコンテンツをファイルに保存できます。 セット内容 コマンドレット。

PowerShell でファイルを読み取るには、 取得コンテンツ コマンドレットを使用し、ファイル内の行を置き換えた後、コンテンツをファイルに保存するには、 セット内容 コマンドレット。

この記事では、PowerShell の replace() メソッドまたは replace 演算子を使用してファイル内の行を置き換える方法について説明します。

Replace() メソッドを使用した PowerShell のファイル内の行の置換

PowerShell には、 取得コンテンツ ファイルの内容を読み取るコマンドレット。 PowerShell String 組み込みメソッドの使用 交換()、ファイル内の行を簡単に置換したり、ファイル内のテキストを置換したりできます。

# Read the file content using the Get-Content
$filecontent = Get-Content -Path D:PSPS-String.txt -Raw

#Print the file content
$filecontent

# Replace a line in a file

$str.Replace("Refer the link https://shellgeek.com/tag/active-directory-2/","Refer the link Https://ShellGeek.com/")

# Save the replace line in a file  

$filecontent.Replace("Refer the link https://shellgeek.com/tag/active-directory-2/","Refer the link Https://ShellGeek.com/") | Set-Content -Path D:PSPS-String.txt                  

    

上記の PowerShell スクリプトでは、 取得コンテンツ コマンドレットは、 パラメータを使用して読み取るテキスト ファイルを指定し、 ファイルの内容全体を文字列として変数に格納するパラメータ $filecontent.

ファイル内の行を置き換えるには、次を使用します PowerShell の置換 () 2 つの引数を取るメソッド。 検索する文字列と、見つかったテキストに置き換える文字列。

ファイル内の行を置換した後、出力を セット内容 ファイルを保存するコマンドレット。

ファイル内の行を置き換える上記の PowerShell スクリプトの出力は次のとおりです。

クールなヒント: PowerShell で文字列内の特殊文字を置き換える方法!

PowerShell replace Operator を使用してファイル内の行を置き換えます

を使用して 交換 演算子、ファイル内の行を置き換えることができます。 使用 取得コンテンツ file を使用してファイルの内容を読み取り、変数 $filecontent に保存します。

PowerShell の replace 演算子を使用して、 $filecontent 検索する行とその引数内で置き換える行を置き換えます。

# Read the file content using Get-Content
$filecontent = Get-Content -Path D:PSPS-String.txt -Raw

$filecontent

# Use replace operator to replace a line in file
$filecontent -replace 'Refer the link https://shellgeek.com/tag/active-directory-2/','Refer the link 
 Https://ShellGeek.com' 

上記の PowerShell スクリプトでは、 交換 演算子は行を置き換えます “リンクを参照してください https://shellgeek.com/tag/active-directory-2/” と “リンクを参照 https://ShellGeek.com

ファイル内の行を置き換えた後の上記の PowerShell スクリプトの出力は次のとおりです。

PS C:> $filecontent = Get-Content -Path D:PSPS-String.txt -Raw                                                       
PS C:>                                                                                                                 

PS C:> $filecontent
Hello SysAdmin, Welcome to PowerShell Programming.
Let's begin your Active Directory management using the PowerShell journey here.

Refer the link https://shellgeek.com/tag/active-directory-2/

PS C:>                                                                                                                 

PS C:> $filecontent -replace 'Refer the link https://shellgeek.com/tag/active-directory-2/','Refer the link Https://ShellGeek.com'                                                                                                             

Hello SysAdmin, Welcome to PowerShell Programming.
Let's begin your Active Directory management using the PowerShell journey here.

Refer the link Https://ShellGeek.com

PS C:>                                                                                                                                                                                                                                                              

結論

PowerShell の replace() メソッドまたは PowerShell の replace 演算子を使用してファイル内の行を置き換える方法に関する上記の記事がお役に立てば幸いです。

PowerShell Active Directory コマンドと PowerShell の基本に関するその他のトピックについては、ShellGeek のホームページを参照してください。

The post PowerShell でファイルの行を置換 appeared first on Gamingsym Japan.