Skip to content

Add New-TemporaryDirectory cmdlet#27549

Open
KirtiRamchandani wants to merge 2 commits into
PowerShell:masterfrom
KirtiRamchandani:feat/new-temporary-directory
Open

Add New-TemporaryDirectory cmdlet#27549
KirtiRamchandani wants to merge 2 commits into
PowerShell:masterfrom
KirtiRamchandani:feat/new-temporary-directory

Conversation

@KirtiRamchandani
Copy link
Copy Markdown

PR Summary

Add New-TemporaryDirectory to Microsoft.PowerShell.Utility.

The new cmdlet follows the shape of New-TemporaryFile: it supports ShouldProcess, uses low confirmation impact, exports from the Utility module on Windows and Unix, and returns a System.IO.DirectoryInfo for a unique directory under the system temp path.

PR Context

Fixes #25754.

The working group recommended adding New-TemporaryDirectory so users can create temporary directories without calling [System.IO] directly. That matters in constrained and restricted language modes where direct .NET type access is not available.

Validation

  • git diff --check
  • Parsed the touched PowerShell scripts and module manifests with System.Management.Automation.Language.Parser
  • Start-PSBuild -Clean -PSModuleRestore -UseNuGetOrg
  • Start-PSPester -Path test/powershell/Modules/Microsoft.PowerShell.Utility/New-TemporaryDirectory.Tests.ps1 -Tag CI -SkipTestToolBuild -UseNuGetOrg (4 passed)
  • Start-PSPester -Path test/powershell/engine/Basic/DefaultCommands.Tests.ps1 -Tag CI -UseNuGetOrg (290 passed)
  • Manual built-shell smoke test confirmed New-TemporaryDirectory returns System.IO.DirectoryInfo, creates the directory, and places it under [System.IO.Path]::GetTempPath()

PR Checklist

  • I have read the CONTRIBUTING document.
  • If this PR is work in progress, I am opening the PR as a draft.
  • The PR is made against the master branch.
  • My commit messages follow the repository guidelines.
  • I have added tests or validation that prove my fix is effective or that my feature works.
  • I have added necessary documentation, if appropriate.
  • Breaking changes
    • None
  • User-facing changes
  • Testing - New and feature
    • Added focused Pester coverage

Copilot AI review requested due to automatic review settings June 1, 2026 07:31
@KirtiRamchandani KirtiRamchandani requested a review from a team as a code owner June 1, 2026 07:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds the new New-TemporaryDirectory cmdlet to PowerShell Utility, wires it into module exports/default command verification, and introduces Pester coverage + documentation status update.

Changes:

  • Adds New-TemporaryDirectory cmdlet implementation (NewTemporaryDirectoryCommand.cs).
  • Exports the cmdlet from Microsoft.PowerShell.Utility (Windows/Unix manifests) and registers it in default-command verification.
  • Adds Pester tests for the cmdlet and updates the cmdlet test-status documentation table.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/powershell/engine/Basic/DefaultCommands.Tests.ps1 Adds New-TemporaryDirectory to the default cmdlet/alias verification matrix.
test/powershell/Modules/Microsoft.PowerShell.Utility/New-TemporaryDirectory.Tests.ps1 Introduces Pester coverage for creation/uniqueness/WhatIf/output type.
src/Modules/Windows/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 Exports New-TemporaryDirectory on Windows.
src/Modules/Unix/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 Exports New-TemporaryDirectory on Unix.
src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryDirectoryCommand.cs Implements the cmdlet with SupportsShouldProcess and error handling.
docs/testing-guidelines/PowerShellCoreTestStatus.md Marks New-TemporaryDirectory as delivered/tested in the status table.

Comment on lines +44 to +46
It "has an OutputType of System.IO.DirectoryInfo" {
(Get-Command New-TemporaryDirectory).OutputType | Should -BeExactly "System.IO.DirectoryInfo"
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed in 2777e98c0: the test now checks (Get-Command New-TemporaryDirectory).OutputType.Name with Should -Contain "System.IO.DirectoryInfo", so it handles the output type collection correctly.

Comment on lines +21 to +22
string tempPath = Path.GetTempPath();
if (ShouldProcess(tempPath))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed in 2777e98c0: the cmdlet now generates the candidate temp directory path before ShouldProcess and uses that full path as the target. The focused Pester run also shows the -WhatIf message now points at the generated tmp_* directory path.

Comment on lines +29 to +37
catch (IOException ioException)
{
ThrowTerminatingError(
new ErrorRecord(
ioException,
"NewTemporaryDirectoryWriteError",
ErrorCategory.WriteError,
tempPath));
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed in 2777e98c0: directory creation now wraps both IOException and UnauthorizedAccessException in terminating ErrorRecords, using PermissionDenied for access failures and the generated directory path as the target.

@kilasuit kilasuit added the WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module label Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

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

3 participants