2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

一時フォルダ内で作業する

Last updated at Posted at 2019-09-03

指定した動作を一時フォルダ内で実行する関数を作った。

function RunInTemporaryFolder([scriptblock]$Process) {    
    $Current = Get-Location

    $WorkFolder =
        New-TemporaryFile |
            ForEach-Object {
                Remove-Item $_
                mkdir $_
            }

    Set-Location -Path $WorkFolder

    & $Process

    Set-Location -Path $Current

    Remove-Item -Path $WorkFolder -Recurse -Force
}

使用方法・実行例

使用方法

RunInTemporaryFolder({
    # 実行する動作
})

実行例

RunInTemporaryFolder({
    Get-Location
})

実行結果

Path
----
C:\Users\ix523\AppData\Local\Temp\tmpCA44.tmp

参考

Powershellテンポラリフォルダとテンポラリファイルの作り方 - 迷惑堂本舗

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?