2

I have a .txt with the names of over 1000 files I want to delete. My .txt file does not have file paths. The files I want to delete are spread throughout multiple folders but they are all in the same drive. Is there any way to use powershell or command prompt to search for all files within my drive with the same name as what is listed in my .txt file and delete them?

  • Yes this is possible, what have you tried so far? – Brien Foss Jan 05 '18 at 02:23
  • https://superuser.com/questions/355584/how-to-delete-files-from-a-folder-using-a-list-of-file-names-in-windows. I tried following all the ideas in here and I could get the command to run but nothing would delete. I am a complete newbie with this stuff and have very little knowledge. – Michael Marchsteiner Jan 05 '18 at 02:27
  • consider taking a look here [Recursive file search using Powershell](https://stackoverflow.com/questions/8677628/recursive-file-search-using-powershell). When you’ve successfully figured out how to recursively search the folders of the drive, then build upon that skill and begin checking for the filenames that are contained in your text file (try populating an Array). When you think you’ve got it right, copy them to a singular location or write the full file path to a text file (verify), when that looks right, instead of copying them, delete them. This will allow you to test along the way. – Brien Foss Jan 05 '18 at 02:38
  • Also, it would be wise to choose a narrower folder path to test with considering your end goal. It would be quite problematic if you got it wrong and deleted more than you intended. – Brien Foss Jan 05 '18 at 02:47
  • First off - thank you very much for pointing me in the right direction. I am a little stuck though. I did a few recursive searches and they seemed to go well. I populated an array using my .txt file like you hinted. I named my array $remove I tried combining my array with my recursive search and got an error though. Get-ChildItem : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'. Specified method is not supported. What I tried was: Get-ChildItem -Path "F:\Folder" -Filter $remove -Recurse – Michael Marchsteiner Jan 05 '18 at 03:09
  • Figured that part out. I just had to use -include instead of -filter. Almost there now. Mt final issue is that some of my files have square brackets which interfere with the remove-item. I read I need to insert -literalpath but I can't figure out where it should go. This is what I have now: Get-ChildItem -Path "F:\Folder" -include $remove -Recurse | Remove-Item -force – Michael Marchsteiner Jan 05 '18 at 05:01
  • As an example my .txt file has xyz [!].txt as a value. I populated an array using the .txt file so my array now has xyz [!].txt as a value. When I run my command the square brackets in the array prevent the command from finding the file named xyz [!].txt and deleting it. – Michael Marchsteiner Jan 05 '18 at 05:25
  • what version of powershell you using? – Ricc Babbitt Jan 05 '18 at 07:55
  • 1
    I ended up writing a command to remove all the square brackets from my file names first and that did the trick. Thank you. – Michael Marchsteiner Jan 05 '18 at 14:59

2 Answers2

2

Assuming you're PowerShell prompt is currently set at the root location from which you want to start your search and the file is in the same directory:

gc .\MyListOfFilesIWantToDelete.txt | %{gci $_ -Recurse | Remove-Item -WhatIf}

Note, you'll have to remove the -whatif

Or, let's say your file is somewhere else where you have PowerShell opened (eg: ~/Documents), and you want to scan your D: drive. This should work:

gc .\MyListOfFilesIWantToDelete.txt | %{gci D:\ -Filter $_ -Recurse -ErrorAction SilentlyContinue | Remove-Item -WhatIf}

Note I put SilentlyContinue. This is because you'll see a lot of red if you don't have access to folders in your search path.

Alternatively, you can load up a variable with your list of files..

$thesefiles = gc .\mylistoffilesiwanttodelete.txt

.. and use the Remove-Item cmdlet directly..

Remove-Item -Path D:\Folder -Include $thesefiles -Recurse -WhatIf

or in one swoop without loading a variable:

Remove-Item -Path D:\Folder -Include $(gc .\mylistoffilesiwanttodelete.txt) -Recurse -WhatIf

Again, I'm using -WhatIf for testing. Also, I've noticed different behaviors in the past with get-childitem on different versions of PowerShell. I tested these with 5.1

Ricc Babbitt
  • 355
  • 1
  • 7
  • This solution will only work if file extension exists in the text file. E.g. `file1.txt`, `file2.pdf` – Jelphy Jan 05 '18 at 11:57
  • I don't see anywhere that this is the condition. The only thing mentioned is that the paths to the files are not included in his text file – Ricc Babbitt Jan 05 '18 at 14:51
-1

Change directory from following powershell command

Following command will allow you to delete .txt files in specific directory

Get-ChildItem C:\*.txt -file -r | remove-item