New-TempDirectory

Creates a new temporary directory with a random name.

Syntax

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

Description

A new temporary directory is created in the current user's env:TEMP directory. The directory's name is created using the Path class's GetRandomFileName method.

To add a custom prefix to the directory name, use the Prefix parameter. If you pass in a path, only its name will be used. In this way, you can pass $MyInvocation.MyCommand.Definition (PowerShell 2) or $PSCommandPath (PowerShell 3+), which will help you identify what scripts are leaving cruft around in the temp directory.

Added -WhatIf support in Carbon 2.0.

Related Commands

Parameters

Name Type Description Required? Pipeline Input Default Value
Prefix String

A prefix to use, so you can more easily identify what created the temporary directory. If you pass in a path, it will be converted to a file name.

false false
WhatIf SwitchParameter false false
Confirm SwitchParameter false false
CommonParameters This cmdlet supports common parameters. For more information type
Get-Help about_CommonParameters.

Return Values

System.IO.DirectoryInfo.

EXAMPLE 1

New-TempDirectory

Demonstrates how to create a new temporary directory, e.g. C:\Users\ajensen\AppData\Local\Temp\5pobd3tu.5rn.

EXAMPLE 2

New-TempDirectory -Prefix 'Carbon'

Demonstrates how to create a new temporary directory with a custom prefix for its name, e.g. C:\Users\ajensen\AppData\Local\Temp\Carbon5pobd3tu.5rn.

EXAMPLE 3

New-TempDirectory -Prefix $MyInvocation.MyCommand.Definition

Demonstrates how you can use $MyInvocation.MyCommand.Definition in PowerShell 2 to create a new, temporary directory, named after the currently executing scripts, e.g. C:\Users\ajensen\AppData\Local\Temp\New-TempDirectory.ps15pobd3tu.5rn.

EXAMPLE 4

New-TempDirectory -Prefix $PSCommandPath

Demonstrates how you can use $PSCommandPath in PowerShell 3+ to create a new, temporary directory, named after the currently executing scripts, e.g. C:\Users\ajensen\AppData\Local\Temp\New-TempDirectory.ps15pobd3tu.5rn.