Creates a directory named "archive" relative to the currently executing script's directory, if it doesn't already exist.
#
# Creates a directory named "archive" relative to the currently executing script's directory,
# if it doesn't already exist.
#
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
$archiveDir = "$scriptDir\archive"
if (!(Test-Path -Path $archiveDir))
{
Write-Host "Creating archive directory at: '$archiveDir'"
New-Item -ItemType directory -Path $archiveDir
}
else
{
Write-Host "Archive directory already exists: '$archiveDir'"
}
### Function Example
function Delete-If-Exists
{
param([string] $dir)
if (!(Test-Path -Path $dir))
{
Write-Host "Directory does not exist '$dir'"
}
else
{
Write-Host "Deleting '$dir'"
Remove-Item $dir -recurse
}
}
Delete-If-Exists "C:\path\to\delete"
# Or using .NET
if (![System.IO.Directory]::Exists($tempDir)) {[System.IO.Directory]::CreateDirectory($tempDir)}