facebook twitter hatena line email

Windows/powershell/基本

提供: 初心者エンジニアの簡易メモ
2021年8月19日 (木) 12:24時点におけるAdmin (トーク | 投稿記録)による版

移動: 案内検索

変数

$path = "hoge.txt"

変数で定義したpathを使用

$path = "hoge.txt"
$data = Get-Content $path

テキスト文置換

aaaをcccへ置換

$data = Get-Content hoge.txt | % { $_ -replace "aaa", "ccc" }
$data | Out-File hoge.txt

参考:https://teratail.com/questions/290149

エスケープ文字対応

[]などは、\でエスケープする

$data = Get-Content hoge.txt | % { $_ -replace "aaa\[hoge\]", "ccc[piyo]" }

実行ファイルのdirpath

version3未満の場合

$ScriptDir = $MyInvocation.MyCommand.Path
echo $ScriptDir

version3以上の場合

$ScriptDir = $PSScriptRoot
echo $ScriptDir

参考:https://www.vwnet.jp/Windows/PowerShell/pwd.htm