0

I want to remove a folder and its contents from the C:\ directory. I am using the following command in powershell

$directoryPath = "C:\poc-devops-app"
if (Test-Path -Path $directoryPath) {
    Remove-Item -Path $directoryPath -Recurse -Force -ErrorAction SilentlyContinue
}

The problem is that the command doesn't work if I have a file open that is inside the C:\poc-devops-app directory and it only deletes all the content but not the poc-devops-app directory.

I also tried to separate the command into 2 parts but I get the same result:

$directoryPath = "C:\poc-devops-app"
if (Test-Path -Path $directoryPath) {
    Remove-Item -Path $directoryPath\* -Recurse -Force
    Remove-Item -Path $directoryPath -Force -ErrorAction SilentlyContinue
}

Is there a way to delete the directory and all its contents even if there is a file open?

  • Possible duplicate: [There is no way to delete a file that is currently in use by another process.](https://stackoverflow.com/a/10504739/1701026) – iRon Jul 19 '23 at 05:25
  • Does this answer your question? [Delete a locked file using powershell](https://stackoverflow.com/questions/69518375/delete-a-locked-file-using-powershell) – phuclv Jul 19 '23 at 09:10

0 Answers0