Answers for "powershell check if file exists"

0

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."
 }
Posted by: Guest on September-06-2021
0

powershell check if file exists

Test-Path <path to file> -PathType Leaf
Posted by: Guest on July-01-2021

Browse Popular Code Answers by Language