Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a temporary directory in PowerShell?

PowerShell 5 introduces the New-TemporaryFile cmdlet, which is handy. How can I do the same thing but instead of a file create a directory? Is there a New-TemporaryDirectory cmdlet?

like image 776
Michael Kropat Avatar asked Jan 01 '16 19:01

Michael Kropat


People also ask

How do I create a temp folder in PowerShell?

Within PowerShell there is a built in Cmdlet called New-TemporaryFile . Running this cmdlet simply creates a random 0 byte file in the $ENV:Temp folder in whichever platform you are working in. However, we can borrow the filename created and use it to create a folder instead.

How do I create a temporary working directory?

Use mktemp -d . It creates a temporary directory with a random name and makes sure that file doesn't already exist. You need to remember to delete the directory after using it though.

How do I open a temp folder in PowerShell?

How can I use Windows PowerShell to find the path to the temporary folder? Use the Temp variable, and obtain its value from the Env: PS drive.

How do I create a folder in PowerShell?

To create a folder in PowerShell, use the New-Item cmdlet indicating the location and name of the folder and set the itemType parameter with the value Directory to indicate that you want to create a folder.


2 Answers

I think it can be done without looping by using a GUID for the directory name:

function New-TemporaryDirectory {     $parent = [System.IO.Path]::GetTempPath()     [string] $name = [System.Guid]::NewGuid()     New-Item -ItemType Directory -Path (Join-Path $parent $name) } 

Original Attempt With GetRandomFileName

Here's my port of this C# solution:

function New-TemporaryDirectory {     $parent = [System.IO.Path]::GetTempPath()     $name = [System.IO.Path]::GetRandomFileName()     New-Item -ItemType Directory -Path (Join-Path $parent $name) } 

Analysis Of Possibility Of Collision

How likely is it that GetRandomFileName will return a name that already exists in the temp folder?

  • Filenames are returned in the form XXXXXXXX.XXX where X can be either a lowercase letter or digit.
  • That gives us 36^11 combinations, which in bits is around 2^56
  • Invoking the birthday paradox, we'd expect a collision once we got to around 2^28 items in the folder, which is about 360 million
  • NTFS supports about 2^32 items in a folder, so it is possible to get a collision using GetRandomFileName

NewGuid on the other hand can be one of 2^122 possibilities, making collisions all but impossible.

like image 113
Michael Kropat Avatar answered Sep 20 '22 20:09

Michael Kropat


I also love one-liners, and I'm begging for a downvote here. All I ask is that you put my own vague negative feelings about this into words.

New-TemporaryFile | %{ rm $_; mkdir $_ } 

Depending on the type of purist you are, you can do %{ mkdir $_-d }, leaving placeholder to avoid collisions.

And it's reasonable to stand on Join-Path $env:TEMP $(New-Guid) | %{ mkdir $_ } also.

like image 20
nik.shornikov Avatar answered Sep 22 '22 20:09

nik.shornikov