How to Delete a Folder or File using PowerShell

Use PowerShell to Delete a Single File or Folder

Before start executing the Delete powershell command we need to make sure you are logged in to the server or PC with an account that has full access to the objects you want to delete.

# Using PowerShell commnads to delete a file
Remove-Item -Path "C:\dotnet-helpers\DummyfiletoDelete.txt"

The above command will excute and delete the “DummyfiletoDelete.txt” file which present inside the “C:\dotnet-helpers” location.

Use PowerShell to Delete all File and Folders inside the folder

We can also use wildcard ‘*’ characters to remove multiple items. For example, this command removes all the files in “C:\dotnet-helpers\*.*”

# Using PowerShell commnads to delete all file
Remove-Item -Path "C:\dotnet-helpers\*.*"

# Using PowerShell commnads to delete all file and folders
Remove-Item -Path "C:\dotnet-helpers\*.*" -recurse

Recurse drills down and finds lots more files. The –recurse parameter will allow PowerShell to remove any child items without asking for permission. Additionally, the –force parameter can be added to delete hidden or read-only files.

Using -Force command to delete files force fully

# Using PowerShell commnads to delete all file force fully
Remove-Item -Path "C:\dotnet-helpers\*.*" -Force

The above command will delete all hidden or read-only files from the location “C:\dotnet-helpers\”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.