powershell - テキスト編集

概要

テキストファイルの内容を取得、置換、別名で保存したい。
※末尾に不要な改行をつけたくない。

サンプルコード

テキストファイルの内容を取得、置換、別名で保存

$data_dir = $args[0]

Get-ChildItem -path $data_dir | ForEach-Object {
    $file = $_.FullName
    $contents = Get-Content -Path $file -Encoding UTF8 -Raw
    $contents = $contents -replace "abc","xyz"
    $output_file = $file + ".rep"
    $utf8 = [Text.Encoding]::UTF8
    [System.IO.File]::WriteAllText($output_file, $contents, $utf8)
}