techthoughts2
10/29/2015 - 8:38 PM

Create new directory if not present

Evaluates if a directory is present. if not found, the specified directory will be created.

if (-not(Test-Path -Path $TargetDir -ErrorAction Stop )) {
    Write-Verbose -Message ('Output directory {0} not found. Creating...' -f $TargetDir)
    $newItemSplat = @{
        ItemType    = 'Directory'
        Path        = $TargetDir
        ErrorAction = 'Stop'
    }
    try {
        New-Item @newItemSplat
        Write-Verbose -Message 'Created.'
    }
    catch {
        Write-Warning -Message 'An error was encountered creating the output directory:'
        Write-Error $_
        return
    }
}
else {
    Write-Verbose -Message 'Output directory verified.'
}