# PowerShell script to remove/delete 2 million files on a server

I need to remove 2.5 million log files on a SolarWinds server and wanted a script to do this. Can they be deleted/removed in batches?
LVL 2
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Exchange, AD, SQL, VMware, HPE, 3PAR, FUD, Anti MS Tekhnet, Pro EE, #1Commented:
Very nasty. :X Why not just delete the folder or that is not possible? Perhaps use robocopy to cut the files out of that directory into another directory and then remove?
Powershell or CMD will be fine.

What are the criteria you have for deleting them?

If its simple and you want to do it in batches, consider the simplicity of using the del CMD as you can easily delete file globs.

Ie if the files are dated in their name(I believe they are), you can delete all of october 2016 reliably using a glob tbat matches the date.

CMD Eg.

DEL /Q /F "C:\path\to solar winds logs\*201610*"


Powershell Eg.

Remove-Item -path "C:\path\to solar winds logs\*201610*" -force


If you want to be rid of them all most efficiently then delete the folder using RMDir (RD) and then recreate it using MKDir (MD)

CMD Eg.
RD  /S /Q "C:\path\to solar winds logs\" && MD "C:\path\to solar winds logs\"


Powershell Eg:

Remove-Item -path "C:\path\to solar winds logs\" -force -recurse
New-Item "C:\path\to solar winds logs\" -type directory


Powershell seems slower here in my experiance.

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Director of Information TechnologyCommented:
Here is a PS that I use to keep my FTP and log folders clean of old data.
I have this PS script run using Task Schedulers.

here is a ps I user to delete older files from a directory and I run it as a scheduled task

[code]
#Change the date to your desired time frame
# (get-date).adddays(-30) = 30 days old or older
get-childitem -path 'C:\Users\*******\Downloads\test2' | where {$_.lastwritetime -gt (get-date).adddays(-30) -and -not$_.psiscontainer} |% {remove-item \$_.fullname -force}


[/code]
Infrastructure EngineerAuthor Commented:
Hi Guys, the files are generated by SolarWinds and are RSA Crypto files (2KBs) each. We have pencilled in a Maintenance Window over Xmas and I'll try the PS scripts first, one question. I have run one of the scripts above and was receiving many Access Denied errors whilst running the PS but was logged on to the server as Admin. If the script/s won't work then a manual process might be the alternative.

Thanks guys for the advice and support. Merry Christmas