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