I've created a script which at the end deletes all the associated files, self-destructs by deleting itself, and then it's supposed to delete the folder it is contained in as well.
I've tried several ways of closing Windows Explorer, searching through active processes and killing any related processes, but still cannot successfully delete the folder despite being able to self-destruct the script itself.
Set-Location -Path $PSScriptRoot
Remove-Item -Path $PSScriptRoot\Mobile -Force -Recurse
Remove-Item -Path $PSScriptRoot\NoMobile -Force -Recurse
Remove-Item -Path $MyInvocation.MyCommand.Source -Force -Recurse
Set-Location ..
Remove-Item $foldername -Force -Recurse
The last line throws an error that "The process cannot access the file...because it is being used by another process".
Any thoughts?