In this article, we take a look at how to delete a file using, PowerShell.
Example 1: One Liner: Delete a File
Windows:
Remove-Item -Path "D:/Data/file1.txt" -Force
macOS:
Remove-Item -Path "/Users/c2ctechtv/Desktop/file1.txt"
Example 2: Using PowerShell Script
$filePath = Read-Host "Enter the file path to delete!"
if (Test-Path -Path $filePath) {
Remove-Item -Path $filePath -Force
Write-Host "The file was deleted successfully!"
} else {
Write-Host "File not deleted! Make sure the file exists or the file path is correct!."
}

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Powershell,
- PowerShell: How to Check if a Module is Already Installed
- PowerShell: How to Write Output to a File
- PowerShell: Steps to Connect to connect to Exchange Online
- PowerShell Fix: Get-Help cannot find the Help files for this cmdlet on this computer
- PowerShell Traditional For Loop Example
- How to Run PowerShell Script as Administrator (Elevate)
- Update Powershell Using Command Line
- How to delete a file using PowerShell [Windows/macOS]
- PowerShell: Grep Command Alternative - Select-String
- PowerShell Concatenate String Examples
- How to add Sleep to PowerShell Script
- Connect Azure AD (Active Directory) for PowerShell
- How to Open PowerShell on Mac?
- List of PowerShell Function Commands for Mac
- How to Connect to Azure AD Account using PowerShell on Mac
- Download Google Chrome setup exe file using PowerShell
- Set Environment Variable in PowerShell for Mac
- How to switch to Powershell on Mac Terminal
- How to add sleep in Powershell Script
- PowerShell on Mac: The term get-service is not recognized as a name of a cmdlet, function, script file, or executable program
- How to Identify installed PowerShell version
- How to check PowerShell version
- How to install PowerShell on Mac using Brew
- PowerShell Switch Statement with Examples
- PowerShell ISE Alternative for Mac
More Posts:
- Install Gradle VS Code for Java Projects - Gradle
- Fix - bash: man: command not found - Linux
- Tomcat Manager Default Username and Password - Tomcat
- Create Symbolic Link using Terminal Command - MacOS
- Create Duplicate Line Visual Studio Code (above or below) Example - HowTos
- Stop a Running Command on macOS Terminal - MacOS
- List of Code Snippets/Templates to Eclipse IDE - Eclipse
- Perform Basic Authentication using cURL with Examples - cURL