Skip to content

Add New-TemporaryDirectory cmdlet#26679

Open
AndyBodnar wants to merge 5 commits intoPowerShell:masterfrom
AndyBodnar:add-new-temporarydirectory-cmdlet
Open

Add New-TemporaryDirectory cmdlet#26679
AndyBodnar wants to merge 5 commits intoPowerShell:masterfrom
AndyBodnar:add-new-temporarydirectory-cmdlet

Conversation

@AndyBodnar
Copy link

@AndyBodnar AndyBodnar commented Jan 11, 2026

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

MicrosoftDocs/PowerShell-Docs#12676

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
Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

Since CreateTempSubdirectory() has a parameter for prefix we could expose this with the cmdlet too as an optional parameter.

$tempDir = Net-TempDirectory -Prefix "MyApp"

@AndyBodnar
Copy link
Author

Added the -Prefix parameter. Now you can do:

$tempDir = New-TemporaryDirectory -Prefix "MyApp"

Also removed the unused tempPath variable. Added a test that verifies the directory name starts with the given prefix.

Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

@AndyBodnar Please create new issue in https://github.com/MicrosoftDocs/PowerShell-Docs and add reference in the PR description.

Comment on lines +4 to +15
# This is a Pester test suite to validate the New-TemporaryDirectory cmdlet in the Microsoft.PowerShell.Utility module.

<#
Purpose:
Verify that New-TemporaryDirectory creates a temporary directory.

Action:
Run New-TemporaryDirectory.

Expected Result:
A DirectoryInfo object for the temporary directory is returned.
#>
Copy link
Collaborator

@iSazonov iSazonov Jan 19, 2026

Choose a reason for hiding this comment

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

Please remove unneeded comments.

Copy link
Collaborator

@iSazonov iSazonov Jan 28, 2026

Choose a reason for hiding this comment

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

Please address the request.


$tempDir | Should -Exist
$tempDir | Should -BeOfType System.IO.DirectoryInfo
$tempDir | Should -BeLikeExactly "$([System.IO.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.

Please remove the line. It is implementation detail from .Net and it could be changed there.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@AndyBodnar Please address the request.

/// <summary>
/// The implementation of the "New-TemporaryDirectory" cmdlet.
/// </summary>
[Cmdlet(VerbsCommon.New, "TemporaryDirectory", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Low, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2097032")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

@sdwheeler Could you please create LinkId for new cmdlet?

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jan 19, 2026
@iSazonov iSazonov requested a review from SteveL-MSFT January 19, 2026 03:16
@AndyBodnar
Copy link
Author

AndyBodnar commented Jan 19, 2026

Created docs issue: MicrosoftDocs/PowerShell-Docs#12676

@iSazonov

@iSazonov iSazonov added the PowerShell-Docs needed The PR was reviewed and a PowerShell Docs update is needed label Jan 19, 2026
@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Jan 26, 2026
@AndyBodnar
Copy link
Author

Will get this addressed later today.

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

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log PowerShell-Docs needed The PR was reviewed and a PowerShell Docs update is needed Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants