New-TempDirectory

Creates a new temporary directory.

Syntax

New-TempDirectory [[-Prefix] <String>] [<CommonParameters>]

Description

The temporary directory is created in the current user's TEMP directory (e.g. $env:TEMP). We use [IO.Path]::GetRandomFilename for the directory's name. The directory is returned as a System.IO.DirectoryInfo object.

If/when temporary directories fill up, it can be hard to identify the process(es) responsible. To help identify which tests are creating (and not removing) temporary directories, supply a unique, static name as the value to the Prefix parameter. We recommend the name of the test fixture creating the temp directory.

Parameters

Name Type Description Required? Pipeline Input Default Value
Prefix String

An optional prefix for the temporary directory name. Helps in identifying tests and things that don't properly clean up after themselves.

false false

Return Values

System.IO.DirectoryInfo.

EXAMPLE 1

$tempDir = New-TempDirectory

Demonstrates how to create a temporary directory.

EXAMPLE 2

$tempDir = New-TempDirectory 'Test-NewTempDirectory+'

Demonstrates how to use a custom, identifiable prefix for your temporary directory's name. This is helpful for identfying tests that forget to clean up after themselves.