Force Remove a Folder in-use using Powershell - powershell

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?

This may be not the answer you might be expecting right now but posting it so that it might be helpful if someone comes into this thread.
I happen to face issue in deleting the file then after help from co-worker I found that file was open in the editor or on some other user machine. I got to follow the below document to find out who had the file open and then close those files and tried to perform delete operation and this time it worked as expected. Please find the document to figure out who happen to have the file open which you are trying to delete.
https://techgoeasy.com/how-to-tell-who-has-a-file-open-in-windows/

Related

Issue in Copy-Item in powershell

I used the Copy-Item command to copy a set reports to a server location. It was working fine before but recently more reports have been added to the folder and now most of the times it works fine but in some cases it shows the error:
The target file "$destination" is a directory, not a file
The code I used is:
Copy-Item -Path "$Source" -Destination "$destination" -Recurse -Force
I am not sure why I am not getting this error for every case.
What you have should work but this may work around the issue if there is some limitation with certain shared folder destinations:
Note: Remove the -WhatIf once you confirm the changes it will make are correct. Currently it will only output what files would have been copied.
Get-ChildItem $Source | Foreach-Object {
Copy-Item $_.FullName $destination -Recurse -Force -WhatIf
}
Basically, this enumerates all of the files and folders under the $Source directory, then copies the files or folders individually to the destination.
If you are able to offer the values of $Source and $Destination I or another might be able to offer a solution to your problem, rather than a workaround.

PowerShell Move Files From One Server to Another

I know this has been asked a million times, but I can't seem to find anything that works for me. I don't know if there is a permissions issue or what, but I am trying to move files from one server to another using a PowerShell script in the task scheduler and it worked for about a week before it stopped working. There are no errors in the task scheduler, and I'm not well versed in PowerShell at all, I'm just trying to get something quick and simple for our CMS manager to move her files from the website to a folder on another server.
$ORG = "E:\folders\uploads\" ## enter current source folder
$DEST= "\\server-folder-structure\uploads\" ## enter your destination folder
foreach ($ORG in gci $DEST -include *.doc,*.docx,*.pdf,*.png,*.gif,*.jpg,*.jpeg,*.html,*.htm -recurse)
{
Move-Item -path $ORG -destination $DEST ## Move the files to the destination folder
}
I tried this too, in hopes it would work, but still no files are being moved.
Get-ChildItem E:\folder-structure\uploads\* -Include *.doc,*.docx,*.pdf,*.png,*.gif,*.jpg,*.jpeg,*.html,*.htm -Recurse |ForEach-Object { Move-Item $_.FullName \\server-folder-structure\uploads\ }
Am I doing something wrong? Are there permissions to folders that I need to set that I don't know about? Is PowerShell just not the best way to do this? Thanks in advance.
I believe you're making this harder than it should be (with respect to PowerShell being new to you). You don't need a loop on any of your examples if you want to pipe directly to Move-Item:
$ORG = "E:\folders\uploads\" ## enter current source folder
$DEST = "\\server-folder-structure\uploads\" ## enter your destination folder
$filterFor = "*.doc","*.docx","*.pdf","*.png","*.gif","*.jpg","*.jpeg","*.html","*.htm"
Get-ChildItem $ORG -Include $filterFor -File -Recurse |
Move-Item -Destination $DEST -WhatIf
As for what you tried, and as Mathias pointed out, you would be searching your $DEST location in which the files wouldn't exist as they would only be in $ORG; given that that's the actual source folder.
This would also overwrite you $ORG variable with the current item in your iteration in: foreach ($ORG in gci ..){ ... }.
Meaning, your Move-Item would be invalid.

Unable to delete file location using remove-item because it does not exist

I'm writing a script to be implemented in my desk to delete some files for Skype for Business.
I can get the Get-ChildItem command to find the folder but when I use the remove item it says that the file does not exist.
The code is
$SkypeFile = Get-ChildItem -Path c:\users\env:username\AppData\Local\Microsoft sip_* -Force -Recurse -Directory
Remove-Item $SkypeFile
The error code is:
Remove-item : Cannot find path "c:\New Folder..." (the file location I getfrom Get-ChildItem) because it doest not exist.
From what I understand it is searching in the folder that I have the script in and that why it is now working but I am unsure and stuck on why is it doing that.
EDIT:
I have managed to find out why was it looking in the script file location, I added the path but now I encountered a problem.
The file I am looking for can be in one of 3 folders titled 14.0 or 15.0 or 13.0 and I cannot make the remove file item look for it in those folders it just tries to look inside the folder.

deleting log files with powershell

I have a powershell script which deletes log files but not the locked ones. The files are open with notepad++. Not sure what else to add. Any suggestions?
$logpath = "C:\logs"
Get-ChildItem $logpath -recurse | Remove-Item -force
Your best bet here would probably be to use the sysinternals application handle to get the process ID of the file that is locked and then close that process before continuing with the deletion.
i was able to get it to work by adding a pipeline to exclude the folder and its contents instead of deleting it.

Exclude Folder and contents when using Get-ChildItem

I need some help with a powershell script to exclude certain folders and files.
I have it written so far that it ignores shortcuts and a folder called data, but when the delete command runs, it also deletes all of the content's of the Data folder on the users desktop.
This is the essential part of what I have
$exclude = ('*.lnk', "*data*")
{Get-ChildItem -Path ([environment]::GetFolderPath("Desktop")) -Exclude $exclude -Recurse | Remove-Item -Force -Recurse}
How can I best achieve what I want, this is to have the script run at each startup, remove all desktop content apart from shortcuts and the Data folder+contents. The contents could be anything from exe to mp3 and I do not want to just exclude MP3 from deletion as I may well want them removed from other folders that have been placed on the desktop.
regards