Powershell テキストファイルの1行目に別のテキストファイルの内容を追加する






テキストファイルの1行目に別のテキストファイルの内容を追加するPowershellスクリプト。

C:\temp\test1.htmlというテキストファイルの1行目にC:\temp\headr.txtの内容を追加する。

$htmlFilePath = "C:\temp\test1.html"
$filePath = "C:\temp\header.txt"

$contentA = Get-Content -Path $htmlFilePath -Encoding UTF8

$contentB = Get-Content -Path $filePath -Encoding UTF8

$newContent = $contentB + $contentA

$newContent | Set-Content -Path $htmlFilePath -Encoding UTF8


スクリプト実行前のC:\temp\test1.html
test contents

test contents

test contents

test contents

事前に用意しておいたC:\temp\header.txt
==============================
add header
==============================

スクリプト実行後のC:\temp\test1.html
==============================
add header
==============================
test contents

test contents

test contents

test contents

簡単にファイルのマージができるので、大量のテキストファイルに同じコンテンツを追加したい時に便利。