Delete File If Exists in PowerShell

This tutorial will discuss how to delete a file if it exists using PowerShell.

Delete File If Exists in PowerShell

To delete a file if exists in PowerShell:

  • Use Test-Path cmdlet with if statement to check if file exists at given location.
  • If files exists, use Remove-Item cmdlet to remove the file at given location.


First, we stored the file path in a variable $filePath. Then we used the Test-Path command as a conditional expression in the if statement.

Test-Path checks whether all elements of the specified path exist. If all elements exist, it returns True. If not, it returns False.

As file exists at given location, it evaluated to True, and Remove-Item removed a file and printed the output.

Remove-Item is cmdlet used for delete one or more items. It can remove many different types of items, including files, folders, registry keys, variables, aliases, and functions.

If a file does not exist, it is False, and the command in the else block gets executed.

Let’s rerun the script to confirm whether the file is deleted.

As you can see, it returned another output because the file is already removed and no longer exists.

Delete Read-only File If Exists in PowerShell

By default, Remove-Item does not delete a hidden or read-only file. To remove such a file, we must use the -Force parameter.

That’s it. We hope you have understood how to delete a file if it exists in PowerShell.

Was this post helpful?

Related Posts

  • Convert Month name to number in PowerShell
    23 January

    Convert Month Number to Name in PowerShell

    Table of ContentsUsing the Get-Culture() CmdletConvert the Hardcoded Month Number to the NameConvert the Month Number from a Date to the NameConvert the Current Month Number to the NameUsing the [cultureinfo]::InvariantCulture Property Using the Get-Culture() Cmdlet We can use the Get-Culture cmdlet in the following three possible ways: To Convert the Hardcoded Month Number to […]

  • Delete empty folders in PowerShell
    23 January

    Delete Empty Folders in PowerShell

    Table of ContentsUsing the Remove-Item with Get-ChildItem CmdletUsing the Get-ChildItem and ForEach-ObjectUsing the User-defined Function There are multiple ways that we can use to delete the empty folders in PowerShell but before going into the details, let’s have a look at the following directory structure to know how many and which folders are empty. [crayon-63cee67c6a194484090187/] […]

  • PowerShell- Where-Object with multiple conditions
    23 January

    PowerShell – Where-Object with Multiple Conditions

    Table of ContentsUsing Where-Object Cmdlet with Multiple ConditionsScript BlockComparison Statements Using Where-Object Cmdlet with Multiple Conditions In PowerShell, we have various comparison and logical operators that we can use with the Where-Object cmdlet to apply multiple conditions. Let’s explore a few scenarios below. [crayon-63cee67c6a7d3300515577/] [crayon-63cee67c6a7da920479007/] The above command returned an item where the extension of […]

  • Rename Files with PowerShell
    18 January

    Rename Files with PowerShell

    Table of ContentsRename Single File with PowerShellUsing Rename-Item with/without the -Path ParameterUsing Rename-Item with -Confirm ParameterUsing Rename-Item with -Passthru ParameterRename multiple files in PowerShell Rename Single File with PowerShell Using Rename-Item with/without the -Path Parameter Use the Rename-Item cmdlet to rename a file in PowerShell. [crayon-63cee67c629de197029319/] We used the Rename-Item cmdlet to rename the test.txt […]

  • PowerShell - redirect output to File
    18 January

    PowerShell – Redirect Output to File

    Table of ContentsUsing Out-File CmdletUsing Redirection OperatorsUsing StreamWriter .Net ClassUsing Add-Content CmdletUsing Set-Content Cmdlet Using Out-File Cmdlet Use the Out-File cmdlet to redirect PowerShell output to a file. [crayon-63cee67c6aeb4401085834/] The Get-Process is used to retrieve all the processes that are currently running on our machine (local computer). We can filter this information by specifying parameters; […]

  • PowerShell - Logical operators
    18 January

    PowerShell – Logical Operators

    Table of ContentsUsing Logical Operators in PowerShellUse Logical AND OperatorUse Logical OR OperatorUse Logical EXCLUSIVE OR OperatorUse Logical NOT OperatorUse Multiple Logical Operators Using Logical Operators in PowerShell PowerShell’s logical operator is a potent tool for comparing data and carrying out operations depending on those comparisons. It allows the user to design an if-then scenario […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.