Skip to content

Conversation

@AndyBodnar
Copy link

This adds a New-TemporaryDirectory cmdlet that follows the same pattern as New-TemporaryFile. The Working Group approved this approach in the issue discussion.

The main use case is for folks running in Constrained Language Mode who cannot use [System.IO] calls directly. Right now they have to do a workaround like:

New-TemporaryFile | % { rm $_; ni $_ -ItemType Directory }

Which is awkward. This cmdlet gives them a clean way to create temp directories.

The implementation uses Path.GetRandomFileName() combined with Path.GetTempPath() to generate a unique directory name, then creates it with Directory.CreateDirectory(). I included tests that mirror the existing New-TemporaryFile tests.

Fixes #25754

Adds a new cmdlet that creates a temporary directory, following the same pattern as New-TemporaryFile. This is useful for users in Constrained Language Mode who cannot use [System.IO] directly.

The cmdlet generates a unique directory name using Path.GetRandomFileName() and creates it in the system temp path.

Fixes PowerShell#25754
@AndyBodnar AndyBodnar requested a review from a team as a code owner January 11, 2026 20:26
/// </summary>
protected override void EndProcessing()
{
string tempPath = Path.GetTempPath();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you consider Directory.CreateTempSubdirectory() ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New-TemporaryDirectory or New-TemporaryItem or New-Item -Temporary

2 participants