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
CTCRMInfrastructure EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Mark BillExchange, 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?
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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*"

Open in new window


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

Open in new window


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\"

Open in new window


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

Open in new window


Powershell seems slower here in my experiance.

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
yo_beeDirector 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}

Open in new window


[/code]
CTCRMInfrastructure 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
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
remember to run PS as admin, and set the execution level to remote signed or unrestricted to execute scripts that are not signed wirh a cert.

[code]
 Set-ExecutionPolicy unrestricted
[/code]
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.