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 Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

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