Modules/PSUtils/Functions/Utils.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
28
29
30
31
32
33
# ===========================================================================
# Utils.ps1 ---------------------------------------------------------------
# ===========================================================================

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function New-TemporaryDirectory {

    <#
    .DESCRIPTION
        Creates a folder with a random name in system's temp folder
 
    .OUTPUTS
        Systems.String. Absolute path of created temporary folder
    #>

    
    [CmdletBinding(PositionalBinding=$True)]
    
    [OutputType([System.String])]

    Param()

    $path = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())

    #if/while path already exists, generate a new path
    While(Test-Path $path) {
        $path = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
    }

    #create directory with generated path
    New-Item -Path $path -ItemType Directory 
}