Creates a new temporary directory with a random name.
New-TempDirectory [[-Prefix] <String>] [-WhatIf] [-Confirm] [<CommonParameters>]
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.
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 . |
New-TempDirectory
Demonstrates how to create a new temporary directory, e.g. C:\Users\ajensen\AppData\Local\Temp\5pobd3tu.5rn
.
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
.
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
.
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
.