「Windows/powershell/基本」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→判定) |
|||
| 行11: | 行11: | ||
} | } | ||
</pre> | </pre> | ||
| + | |||
| + | ==dir存在判定== | ||
| + | <pre> | ||
| + | if (Test-Path .git) | ||
| + | { | ||
| + | Write-Output "exists" | ||
| + | } | ||
| + | </pre> | ||
| + | |||
==変数で定義したpathを使用== | ==変数で定義したpathを使用== | ||
$path = "hoge.txt" | $path = "hoge.txt" | ||
2021年8月19日 (木) 14:47時点における版
変数
$path = "hoge.txt"
判定
$condition = true
if ($condition)
{
Write-Output "OK"
}
dir存在判定
if (Test-Path .git)
{
Write-Output "exists"
}
変数で定義した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 C:\Users\hoge1\Desktop\hoge.ps1
version3以上の場合
$ScriptDir = $PSScriptRoot echo $ScriptDir C:\Users\hoge1\Desktop
