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?