How to delete a file using PowerShell [Windows/macOS]


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!."
    }
    
Remove File using PowerShell Example on macOS
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap