Add New-TemporaryDirectory cmdlet - #26679
#26679Add New-TemporaryDirectory cmdlet#26679AndyBodnar wants to merge 5 commits intoPowerShell:masterPowerShell/PowerShell:masterfrom AndyBodnar:add-new-temporarydirectory-cmdletAndyBodnar/PowerShell:add-new-temporarydirectory-cmdletCopy head branch name to clipboard
Conversation
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
iSazonov
left a comment
There was a problem hiding this comment.
Since CreateTempSubdirectory() has a parameter for prefix we could expose this with the cmdlet too as an optional parameter.
$tempDir = Net-TempDirectory -Prefix "MyApp"|
Added the $tempDir = New-TemporaryDirectory -Prefix "MyApp"Also removed the unused |
iSazonov
left a comment
There was a problem hiding this comment.
@AndyBodnar Please create new issue in https://github.com/MicrosoftDocs/PowerShell-Docs and add reference in the PR description.
| # 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. | ||
| #> |
There was a problem hiding this comment.
Please remove unneeded comments.
There was a problem hiding this comment.
Please address the request.
|
|
||
| $tempDir | Should -Exist | ||
| $tempDir | Should -BeOfType System.IO.DirectoryInfo | ||
| $tempDir | Should -BeLikeExactly "$([System.IO.Path]::GetTempPath())*" |
There was a problem hiding this comment.
Please remove the line. It is implementation detail from .Net and it could be changed there.
| /// <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")] |
There was a problem hiding this comment.
@sdwheeler Could you please create LinkId for new cmdlet?
|
Created docs issue: MicrosoftDocs/PowerShell-Docs#12676 |
|
Will get this addressed later today. |
|
@AndyBodnar Are you able to address the comments on this so that we can move forwards with this PR 🙏🏻 |
|
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. |
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:
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