Force Remove a Folder in-use using Powershell












0















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 $PSScriptRootMobile -Force -Recurse

Remove-Item -Path $PSScriptRootNoMobile -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?










share|improve this question




















  • 1





    you may be going about it the wrong way. Can you post the script or the part that is giving you an issue. It looks like you can delete the files based set varaiables in your script.

    – ShanayL
    Jan 2 at 16:18













  • It's the last line which is giving me the issue. The first two removes delete the subfolders, the third deletes the script itself, and the last is intended to delete the folder which was originally storing the script and the subfolders. I can give you an example of the error below.

    – Andreas
    Jan 3 at 8:23











  • Remove-Item : Cannot remove item C:Users<user><folder>40th Anniversary Signatures: The process cannot access the file 'C:Users<user><folder>40th Anniversary Signatures' because it is being used by another process. At C:Users<user><folder>40th Anniversary SignaturesInstall Signatures.ps1:163 char:1 + Remove-Item $foldername -Force -Recurse ~~~ + CategoryInfo : WriteError: (C:Users<user>...sary Signatures:DirectoryInfo) [Remove-Item], IOException + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

    – Andreas
    Jan 3 at 8:31













  • Had to remove a lot of characters above to not go over the character limit. For reference, $foldername is actually "40th Anniversary Signatures" and some personal information was replaced with <user> and <folder>.

    – Andreas
    Jan 3 at 8:32
















0















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 $PSScriptRootMobile -Force -Recurse

Remove-Item -Path $PSScriptRootNoMobile -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?










share|improve this question




















  • 1





    you may be going about it the wrong way. Can you post the script or the part that is giving you an issue. It looks like you can delete the files based set varaiables in your script.

    – ShanayL
    Jan 2 at 16:18













  • It's the last line which is giving me the issue. The first two removes delete the subfolders, the third deletes the script itself, and the last is intended to delete the folder which was originally storing the script and the subfolders. I can give you an example of the error below.

    – Andreas
    Jan 3 at 8:23











  • Remove-Item : Cannot remove item C:Users<user><folder>40th Anniversary Signatures: The process cannot access the file 'C:Users<user><folder>40th Anniversary Signatures' because it is being used by another process. At C:Users<user><folder>40th Anniversary SignaturesInstall Signatures.ps1:163 char:1 + Remove-Item $foldername -Force -Recurse ~~~ + CategoryInfo : WriteError: (C:Users<user>...sary Signatures:DirectoryInfo) [Remove-Item], IOException + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

    – Andreas
    Jan 3 at 8:31













  • Had to remove a lot of characters above to not go over the character limit. For reference, $foldername is actually "40th Anniversary Signatures" and some personal information was replaced with <user> and <folder>.

    – Andreas
    Jan 3 at 8:32














0












0








0


0






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 $PSScriptRootMobile -Force -Recurse

Remove-Item -Path $PSScriptRootNoMobile -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?










share|improve this question
















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 $PSScriptRootMobile -Force -Recurse

Remove-Item -Path $PSScriptRootNoMobile -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?







powershell windows-10 folder






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 16:15









Rob

37.7k21103131




37.7k21103131










asked Jan 2 at 15:57









AndreasAndreas

1




1








  • 1





    you may be going about it the wrong way. Can you post the script or the part that is giving you an issue. It looks like you can delete the files based set varaiables in your script.

    – ShanayL
    Jan 2 at 16:18













  • It's the last line which is giving me the issue. The first two removes delete the subfolders, the third deletes the script itself, and the last is intended to delete the folder which was originally storing the script and the subfolders. I can give you an example of the error below.

    – Andreas
    Jan 3 at 8:23











  • Remove-Item : Cannot remove item C:Users<user><folder>40th Anniversary Signatures: The process cannot access the file 'C:Users<user><folder>40th Anniversary Signatures' because it is being used by another process. At C:Users<user><folder>40th Anniversary SignaturesInstall Signatures.ps1:163 char:1 + Remove-Item $foldername -Force -Recurse ~~~ + CategoryInfo : WriteError: (C:Users<user>...sary Signatures:DirectoryInfo) [Remove-Item], IOException + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

    – Andreas
    Jan 3 at 8:31













  • Had to remove a lot of characters above to not go over the character limit. For reference, $foldername is actually "40th Anniversary Signatures" and some personal information was replaced with <user> and <folder>.

    – Andreas
    Jan 3 at 8:32














  • 1





    you may be going about it the wrong way. Can you post the script or the part that is giving you an issue. It looks like you can delete the files based set varaiables in your script.

    – ShanayL
    Jan 2 at 16:18













  • It's the last line which is giving me the issue. The first two removes delete the subfolders, the third deletes the script itself, and the last is intended to delete the folder which was originally storing the script and the subfolders. I can give you an example of the error below.

    – Andreas
    Jan 3 at 8:23











  • Remove-Item : Cannot remove item C:Users<user><folder>40th Anniversary Signatures: The process cannot access the file 'C:Users<user><folder>40th Anniversary Signatures' because it is being used by another process. At C:Users<user><folder>40th Anniversary SignaturesInstall Signatures.ps1:163 char:1 + Remove-Item $foldername -Force -Recurse ~~~ + CategoryInfo : WriteError: (C:Users<user>...sary Signatures:DirectoryInfo) [Remove-Item], IOException + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

    – Andreas
    Jan 3 at 8:31













  • Had to remove a lot of characters above to not go over the character limit. For reference, $foldername is actually "40th Anniversary Signatures" and some personal information was replaced with <user> and <folder>.

    – Andreas
    Jan 3 at 8:32








1




1





you may be going about it the wrong way. Can you post the script or the part that is giving you an issue. It looks like you can delete the files based set varaiables in your script.

– ShanayL
Jan 2 at 16:18







you may be going about it the wrong way. Can you post the script or the part that is giving you an issue. It looks like you can delete the files based set varaiables in your script.

– ShanayL
Jan 2 at 16:18















It's the last line which is giving me the issue. The first two removes delete the subfolders, the third deletes the script itself, and the last is intended to delete the folder which was originally storing the script and the subfolders. I can give you an example of the error below.

– Andreas
Jan 3 at 8:23





It's the last line which is giving me the issue. The first two removes delete the subfolders, the third deletes the script itself, and the last is intended to delete the folder which was originally storing the script and the subfolders. I can give you an example of the error below.

– Andreas
Jan 3 at 8:23













Remove-Item : Cannot remove item C:Users<user><folder>40th Anniversary Signatures: The process cannot access the file 'C:Users<user><folder>40th Anniversary Signatures' because it is being used by another process. At C:Users<user><folder>40th Anniversary SignaturesInstall Signatures.ps1:163 char:1 + Remove-Item $foldername -Force -Recurse ~~~ + CategoryInfo : WriteError: (C:Users<user>...sary Signatures:DirectoryInfo) [Remove-Item], IOException + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

– Andreas
Jan 3 at 8:31







Remove-Item : Cannot remove item C:Users<user><folder>40th Anniversary Signatures: The process cannot access the file 'C:Users<user><folder>40th Anniversary Signatures' because it is being used by another process. At C:Users<user><folder>40th Anniversary SignaturesInstall Signatures.ps1:163 char:1 + Remove-Item $foldername -Force -Recurse ~~~ + CategoryInfo : WriteError: (C:Users<user>...sary Signatures:DirectoryInfo) [Remove-Item], IOException + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

– Andreas
Jan 3 at 8:31















Had to remove a lot of characters above to not go over the character limit. For reference, $foldername is actually "40th Anniversary Signatures" and some personal information was replaced with <user> and <folder>.

– Andreas
Jan 3 at 8:32





Had to remove a lot of characters above to not go over the character limit. For reference, $foldername is actually "40th Anniversary Signatures" and some personal information was replaced with <user> and <folder>.

– Andreas
Jan 3 at 8:32












1 Answer
1






active

oldest

votes


















0














Silly question: have you confirmed that the value of $foldername is the folder you want to delete? It's not shown in the script, and may have been set to some other file or folder name that's in use.






share|improve this answer
























  • I actually just used $foldername as a placeholder and put the path there instead. While troubleshooting, I inserted Get-Location after each Set-Location to verify that I am where (I think) I should be in order for the following steps to continue successfully.

    – Andreas
    Jan 3 at 8:22











  • Process Explorer link can help you find which process is locking a folder.

    – Rich Moss
    Jan 9 at 20:06











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009351%2fforce-remove-a-folder-in-use-using-powershell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Silly question: have you confirmed that the value of $foldername is the folder you want to delete? It's not shown in the script, and may have been set to some other file or folder name that's in use.






share|improve this answer
























  • I actually just used $foldername as a placeholder and put the path there instead. While troubleshooting, I inserted Get-Location after each Set-Location to verify that I am where (I think) I should be in order for the following steps to continue successfully.

    – Andreas
    Jan 3 at 8:22











  • Process Explorer link can help you find which process is locking a folder.

    – Rich Moss
    Jan 9 at 20:06
















0














Silly question: have you confirmed that the value of $foldername is the folder you want to delete? It's not shown in the script, and may have been set to some other file or folder name that's in use.






share|improve this answer
























  • I actually just used $foldername as a placeholder and put the path there instead. While troubleshooting, I inserted Get-Location after each Set-Location to verify that I am where (I think) I should be in order for the following steps to continue successfully.

    – Andreas
    Jan 3 at 8:22











  • Process Explorer link can help you find which process is locking a folder.

    – Rich Moss
    Jan 9 at 20:06














0












0








0







Silly question: have you confirmed that the value of $foldername is the folder you want to delete? It's not shown in the script, and may have been set to some other file or folder name that's in use.






share|improve this answer













Silly question: have you confirmed that the value of $foldername is the folder you want to delete? It's not shown in the script, and may have been set to some other file or folder name that's in use.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 0:07









Rich MossRich Moss

31426




31426













  • I actually just used $foldername as a placeholder and put the path there instead. While troubleshooting, I inserted Get-Location after each Set-Location to verify that I am where (I think) I should be in order for the following steps to continue successfully.

    – Andreas
    Jan 3 at 8:22











  • Process Explorer link can help you find which process is locking a folder.

    – Rich Moss
    Jan 9 at 20:06



















  • I actually just used $foldername as a placeholder and put the path there instead. While troubleshooting, I inserted Get-Location after each Set-Location to verify that I am where (I think) I should be in order for the following steps to continue successfully.

    – Andreas
    Jan 3 at 8:22











  • Process Explorer link can help you find which process is locking a folder.

    – Rich Moss
    Jan 9 at 20:06

















I actually just used $foldername as a placeholder and put the path there instead. While troubleshooting, I inserted Get-Location after each Set-Location to verify that I am where (I think) I should be in order for the following steps to continue successfully.

– Andreas
Jan 3 at 8:22





I actually just used $foldername as a placeholder and put the path there instead. While troubleshooting, I inserted Get-Location after each Set-Location to verify that I am where (I think) I should be in order for the following steps to continue successfully.

– Andreas
Jan 3 at 8:22













Process Explorer link can help you find which process is locking a folder.

– Rich Moss
Jan 9 at 20:06





Process Explorer link can help you find which process is locking a folder.

– Rich Moss
Jan 9 at 20:06




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009351%2fforce-remove-a-folder-in-use-using-powershell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter