Functions/New-TemporaryFolder.ps1
<#
.SYNOPSIS This function creates a folder with the specified name. .DESCRIPTION This function creates a folder with the specified name. If the folder name is provided, the folder is created in the $env:TEMP directory. #> function New-TemporaryFolder { [CmdletBinding()] [OutputType([System.IO.FileInfo])] param ( # The name of the folder to create. [Parameter(Mandatory=$true, ParameterSetName="name")] [ValidateNotNullOrEmpty()] [String]$folderName, # The path of the folder to create. [Parameter(Mandatory=$true, ParameterSetName="path")] [ValidateNotNullOrEmpty()] [String]$folderPath ) # Generate the folder path if ($PSCmdlet.ParameterSetName -eq "name") { $folderPath = Join-Path -Path $env:TEMP -ChildPath $folderName } # Create the folder if it doesn't exist if (!(Test-Path -Path $folderPath -ErrorAction SilentlyContinue)) { try { $folder = New-Item -ItemType Directory -Path $folderPath } catch { Write-Error "Exception while creating folder '$($folderPath)'.`r`n$($_.Exception.Message)" return $null } if (!$folder) { Write-Error "Failed to create folder '$($folderPath)'." return $null } } else { Write-Warning "Temporary folder '$($folderPath)' already exists." $folder = Get-Item -Path $folderPath } # Return the folder return $folder } |