PowerShell を使用してファイルにデータを追加する方法
-
PowerShell における
Add-Content
の基本構文 -
PowerShell でファイルにテキストを追加するための
Add-Content
の使用 - PowerShell で新しい行にデータを追加するための(`n)演算子の使用
-
PowerShell で一つのファイルの内容を別のファイルに追加するための
Add-Content
コマンドの使用 -
PowerShell で読み取り専用ファイルにデータを追加するための
Add-Content
コマンドの使用

PowerShell の Add-Content
コマンドは、ファイルにコンテンツを追加します。コマンドでコンテンツを指定するか、Get-Content
を使用してファイルの内容を取得して追記することができます。
Add-Content
コマンドレットは、テキストをファイルに追加したり、文字列をファイルに追加したりします。
PowerShell における Add-Content
の基本構文
Windows PowerShell の Add-Content
コマンドレットは、ファイルにコンテンツを追加し、ファイルにテキストを追加します。
Add-Content
[-Path] <string[]>
[-Value] <Object[]>
[-PassThru]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Force]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-NoNewline]
[-Encoding <Encoding>]
[-AsByteStream]
[-Stream <string>]
[<CommonParameters>]
PowerShell でファイルにテキストを追加するための Add-Content
の使用
例えば、ディレクトリに Get-DateFile.txt
というテキストファイルがあるとします。
Get-DateFile.txt
で新しいファイルを作成し、いくつかのテストデータを追加します。
Get-DataFile.txt:
Example illustration about appending text to
Add-Content -Path .\Get-DateFile.txt "End of file"
Add-Content
コマンドレットは、現在のディレクトリの -Path
パラメータで指定されたファイルの末尾に End of file
文字列を追加します。
出力:
Example illustration about appending text to End of file
PowerShell で新しい行にデータを追加するための(`n)演算子の使用
ファイルに新しい行でデータを追加するには、新しい行演算子(`n)を使用します。
Add-Content -Path .\Get-DateFile.txt "`nSecond line starts here..."
出力:
Example illustration about appending text to End of file
Second line starts here…
PowerShell で一つのファイルの内容を別のファイルに追加するための Add-Content
コマンドの使用
Add-Content
コマンドは一つのファイルの内容を別のファイルに追加します。
それはファイルの内容を読み取り、変数に割り当て、宛先ファイルに内容を書き込みます。
Add-Content
コマンドは、ファイルが存在しない場合、新しいファイルを作成しながらテキストをファイルに追加します。
# Read file contents to variable
$sourceFileContent = Get-Content -Path .\GetFileProperties.txt
# This line will append the content of one file to another file
# If the file does not exist, the Add-Content command will create a new file
Add-Content -Path .\Destination_File.txt -Value $sourceFileContent
一つのファイルの内容を別のファイルに追加するには、PowerShell の Get-Content
コマンドレットが Path
パラメータで指定されたファイルの内容を取得します。
その後、ファイルの内容を読み取り、変数 $sourceFileContent
に保存します。
PowerShell の Add-Content
コマンドレットは、-Value
パラメータで指定されたソースファイルの内容を追加します。
Add-Content
コマンドはファイルが存在しない場合、新しいファイルを作成し、内容をコピーします。
PowerShell で読み取り専用ファイルにデータを追加するための Add-Content
コマンドの使用
# Create a new file
New-Item -Path .\TempROFile.txt -ItemType File
# Set file as read-only
Set-ItemProperty -Path .\TempROFile.txt -Name IsReadOnly -Value $True
# Get file details
Get-ChildItem -Path .\TempROFile.txt
# Appends the line to file
Add-Content -Path .\TempROFile.txt -Value 'End of File' -Force
最初のコマンドは、PowerShell の New-Item
コマンドレットを使用して新しいファイルを作成します。
PowerShell の Set-ItemProperty
コマンドは、指定したファイルの IsReadOnly
プロパティを true に設定するために使用されます。
PowerShell の Get-ChildItem
コマンドは、Name
、LastWriteTime
、Length
、および mode
などの指定されたファイルの詳細を取得します。
Add-Content
コマンドレットは -Path
パラメータで指定された読み取り専用ファイルに行を追加します。
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedIn