• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Tachytelic.net

  • Sco Openserver
    • Sco Unix Support
    • SCO Openserver Installation ISOs
    • SCO Openserver Binaries
    • Add a Hard Drive to an Openserver 5 Virtual Machine
    • Install SCO Vision SQL-Retriever ODBC Driver on Windows 10
    • License Expired on Virtual SCO Openserver Installation
    • How to reset the root password on SCO Openserver 5
  • Scripting
    • PowerShell
      • Add leading zeros
      • Check if a File Exists
      • Grep with Powershell
      • Create Environment Variables
      • Test for open Ports
      • Append to a Text File
    • VBScript
      • Check if a File Exists
      • lpad and rpad functions
      • Windows Update E-Mail Notification
  • Office 365
    • Connect to Office 365 with PowerShell
    • Add or remove an email alias using Powershell
    • Change Primary email address of Active Directory user
    • How to hide an AD user from the Global Address List
    • How to hide mail contacts from the Global Address List
    • Change the primary email address for an account with PowerShell
    • Change Primary email address of an AD User
    • Grant a single user access to access to all calendars
    • Forward email to an external address using Powershell
    • Convert shared mailbox to user mailbox with Powershell
  • Get in Touch
  • About Me
    • Privacy Policy

Check if File Exists with Powershell

May 1, 2019 by Paulie 2 Comments

It’s very easy to check if a file exists with Powershell and take an action if so, here are some examples:

Check if a file exists

$fileToCheck = "C:\tmp\test.txt"
if (Test-Path $fileToCheck -PathType leaf)
{
    #do some stuff
}

Delete file if it exists

$fileToCheck = "C:\tmp\test.txt"
if (Test-Path $fileToCheck -PathType leaf)
{
    Remove-Item $fileToCheck
}

An even shorter method….

$fileToCheck = "c:\tmp\test.txt"
Remove-Item $fileToCheck -ErrorAction Ignore

Check if a file exists and get the content

$fileToCheck = "C:\tmp\test.txt"
if (Test-Path $fileToCheck -PathType leaf) {Get-Content $fileToCheck}

Check if a file exists and print a message if it does or does not exist

$fileToCheck = "C:\tmp\test.txt"
if (Test-Path $fileToCheck -PathType leaf) 
{"File does Exist"}
else
{"File does not exist"}

Check if a file exists and show the properties of the file

$fileToCheck = "C:\tmp\test.txt"
if (Test-Path $fileToCheck -PathType leaf)
{
    $file = Get-Item $fileToCheck
    $file.FullName
    $file.LastAccessTime
    $file.Length
    $file.Extension
    #etc...
}

Check if a file exists and move it to a new location

$fileToCheck = "C:\tmp\test.txt"
$newPath = "c:\tmp\newFolder"
if (Test-Path $fileToCheck -PathType leaf) 
{
    Move-Item -Path $fileToCheck -Destination $newPath
    "$fileToCheck moved to $newPath"
}

It’s obvious from the above examples that Test-Path simply returns a Boolean (i.e. True or False) and executing it on its own will produce exactly that:

Image showing result from Powershell "Test-Path" to check if a file exists.

The PathType parameter is not strictly required, but it is useful if you want to be sure the check you are performing is actually a file, not a directory.

You can check for a directory by setting PathType to “container”

Filed Under: How To Tagged With: Powershell

Reader Interactions

Comments

  1. Mister-X says

    June 5, 2020 at 2:18 pm

    Hi
    Nice, is there a command to check the content of a file ? I download a file and at the end of this file is the command: Copying finished
    I would like to have an email sent, when this command appears, is it possible ?

    Thanks for help

Trackbacks

  1. Function to check if a file exists in VBScript says:
    May 1, 2019 at 10:20 pm

    […] Now that you know how to check for a file in VBScript, read up on how to check if a file exists with Powershell. […]

Leave a Reply Cancel reply

Primary Sidebar

Link to my LinkedIn Profile

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 107 other subscribers.

Go to mobile version