powershell if file exists
# Create-NewFile.ps1
# Full path of the file
$file = 'c:\temp\important_file.txt'
#If the file does not exist, create it.
if (-not(Test-Path -Path $file -PathType Leaf)) {
try {
$null = New-Item -ItemType File -Path $file -Force -ErrorAction Stop
Write-Host "The file [$file] has been created."
}
catch {
throw $_.Exception.Message
}
}
# If the file already exists, show the message and do nothing.
else {
Write-Host "Cannot create [$file] because a file with that name already exists."
}