Skip to content

Add New-TemporaryDirectory cmdlet - #26679

Closed
Andy (AndyBodnar) wants to merge 5 commits into
PowerShell:masterfrom
AndyBodnar:add-new-temporarydirectory-cmdlet
Closed

Andy (AndyBodnar) wants to merge 5 commits into
PowerShell:masterfrom
AndyBodnar:add-new-temporarydirectory-cmdlet

Conversation

@AndyBodnar

@AndyBodnar Andy (AndyBodnar) commented Jan 11, 2026 •

Copy link
Copy Markdown

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
Andy (AndyBodnar) requested a review from a team as a code owner January 11, 2026 20:26

@iSazonov Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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.

@iSazonov Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.
#>

@iSazonov Ilya (iSazonov) Jan 19, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please remove unneeded comments.

@iSazonov Ilya (iSazonov) Jan 28, 2026 •

Copy link
Copy Markdown
Collaborator

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

Choose a reason for hiding this comment

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

Andy (@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
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sean Wheeler (@sdwheeler) Could you please create LinkId for new cmdlet?

@iSazonov Ilya (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
@AndyBodnar

Andy (AndyBodnar) commented Jan 19, 2026 •

Copy link
Copy Markdown
Author

Created docs issue: MicrosoftDocs/PowerShell-Docs#12676

Ilya (@iSazonov)

@iSazonov Ilya (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
Copy Markdown
Author

Will get this addressed later today.

@kilasuit

Copy link
Copy Markdown
Collaborator

Andy (@AndyBodnar) Are you able to address the comments on this so that we can move forwards with this PR 🙏🏻

@SteveL-MSFT

Copy link
Copy Markdown
Member

The @PowerShell/wg-powershell-cmdlets discussed this. It looks like this PR was first for resolving the issue, but it appears that the author hasn't responded to address feedback. Therefore, we recommend resolving this one as the duplicate and moving forward with #27549 instead.

@SteveL-MSFT Steve Lee (SteveL-MSFT) added the Resolution-Duplicate The issue is a duplicate. label Jun 17, 2026
@microsoft-github-policy-service microsoft-github-policy-service Bot removed the Review - Needed The PR is being reviewed label Jun 17, 2026
@SteveL-MSFT Steve Lee (SteveL-MSFT) moved this from In-Progress-PullRequests to Reviewed in Cmdlets Working Group Jun 17, 2026
@kilasuit Ryan Yates (kilasuit) added the WG-Cmdlets general cmdlet issues label Jul 17, 2026
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 Resolution-Duplicate The issue is a duplicate. WG-Cmdlets general cmdlet issues

Projects

Status: Reviewed

Development

Successfully merging this pull request may close these issues.

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

5 participants