Directory.psm1
<#
.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 $env:TEMP $name } # Create the folder if it doesn't exist if (!(Test-Path -Path $folderPath -ErrorAction SilentlyContinue)) { try { $folder = New-Item -ItemType Directory -Path $folderPath } catch { return $null } if (!$folder) { return $null } } else { Write-Warning "Temporary folder '$($folderPath)' already exists." $folder = Get-Item -Path $folderPath } # Return the folder return $folder } |