Muni Bus

パソコンの操作方法や設定方法を忘れないようにメモしています。ブログを一回引っ越ししているので、所々表示がかなり乱れています・・・

【PowerShell】テンポラリファイル(一時ファイル)を作成する

New-TemporaryFileコマンドレットを使う。使ったらファイル名が返されるのではなく、使った時点でサイズが0のファイルが作成される。このファイルは自動で消されないため、作業が終わったら、手動で削除する必要がある。

PS > $tempfile = New-TemporaryFile
PS > Get-ChildItem $tempfile
    ディレクトリ: C:\Users\○○○\AppData\Local\Temp
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2023/03/19     23:14              0 tmpC27D.tmp
PS > Test-Path -Type Leaf $tempfile
True
PS > Get-Content $tempfile
PS > "ABC" | Out-File -Append $tempfile
PS > Get-Content $tempfile
ABC
PS > "あいう" | Out-File -Append $tempfile
PS > Get-Content $tempfile
ABC
あいう
PS > Remove-Item $tempfile
PS > Test-Path -Type Leaf $tempfile
False