Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add New-TemporaryDirectory cmdlet - #26679

#26679
Closed
AndyBodnar wants to merge 5 commits into
PowerShell:masterPowerShell/PowerShell:masterfrom
AndyBodnar:add-new-temporarydirectory-cmdletAndyBodnar/PowerShell:add-new-temporarydirectory-cmdletCopy head branch name to clipboard
Closed

Add New-TemporaryDirectory cmdlet#26679
AndyBodnar wants to merge 5 commits into
PowerShell:masterPowerShell/PowerShell:masterfrom
AndyBodnar:add-new-temporarydirectory-cmdletAndyBodnar/PowerShell:add-new-temporarydirectory-cmdletCopy head branch name to clipboard

Conversation

@AndyBodnar

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

@iSazonov 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 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.

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

@iSazonov 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 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.

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

@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

AndyBodnar commented Jan 19, 2026

Copy link
Copy Markdown
Author

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

Will get this addressed later today.

@kilasuit

Copy link
Copy Markdown
Collaborator

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

@kilasuit kilasuit added the WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module label Jun 1, 2026
@SteveL-MSFT SteveL-MSFT moved this from Queue to In-Progress-PullRequests in Cmdlets Working Group Jun 17, 2026
@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 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 SteveL-MSFT moved this from In-Progress-PullRequests to Reviewed in Cmdlets Working Group Jun 17, 2026
@kilasuit kilasuit added the WG-Cmdlets general cmdlet issues label Jul 17, 2026
@kilasuit kilasuit removed the WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module 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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.