Private/New-TemporaryFolder.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function New-TemporaryFolder {
    <#
        .SYNOPSIS
            Creates a new temporary folder

        .NOTES
            Author: Aaron Parker
            Twitter: @stealthpuppy
    #>

    [CmdletBinding(SupportsShouldProcess = $true)]
    [OutputType([System.String])]
    param ()

    # Check whether current PowerShell environment matches or is higher than $Version
    try {
        $Folder = "vcredist_$([System.Convert]::ToString((Get-Random -Maximum 65535),16).PadLeft(4,'0')).tmp"
        $T = Join-Path -Path $Env:Temp -ChildPath $Folder
        if ($PSCmdlet.ShouldProcess($T, "New directory.")) {
            $Path = New-Item -Path $T -ItemType "Directory" -ErrorAction "SilentlyContinue"
            Write-Output -InputObject $Path.FullName
        }
    }
    catch {
        throw $_
    }
}