Erase files based on date by using PowerShell

Doctor Scripto

Summary: Honorary Scripting Guy, Sean Kearney, relates his first experience with PowerShell.

Hey, Scripting Guy! Question I was curious just how difficult it is to use Windows PowerShell. For example, how difficult is it to erase files based on the current date?

Hey, Scripting Guy! Answer Honorary Scripting Guy, Sean Kearney, is here today, and I can completely relate to exactly what you’ve said. I once had absolutely zero time to learn about scripting or automation. I was a field technician.

You know the typical work for some field techs. You get in the car, go to the call, hop back in the car, off to the next call, lunch in a drive-thru, sleep in a parking lot. Also, factor in about five to six hours lost to commute.

Imagine a schedule like that for seven+ years and  when do you have time to learn? I believed exactly the same thing about Windows PowerShell until I found myself in a position where I had to use it.

I clearly remember the day, too. It was sometime late in 2007. I got a call from a client. “There’s a vendor application filling up the server with logs. Please find some way to get rid of the old data. Anything over 24 hours old is garbage.”

“Okay,” I thought to myself. “I’m not a developer or coder, but maybe there’s a utility on the Internet, something from Sysinternals. If I have to spend two hours to learn how to write something, I’ll do it.”

Well, I got on the site, and the first thing that I did was hit the local search engines for “Erase old files based on date” (or something along those lines). I ran into an article on Hey Scripting Guys that showed how to remove files over seven (7) days old from a folder.

It showed this line in PowerShell to get a list of the files ending in .LOG:

Get-Childitem C:\Foldername\*.LOG

It then showed two additional lines to remove that content. One line grabs the current date, and the other filters the list.

$Now=Get-Date

Get-Childitem C:\Foldername\*.LOG | Where-Object { $_.LastWriteTime –lt $Now.AddDays(7) }

I didn’t really understand much other than, “There’s a folder, and this is supposed to show stuff over seven days old. I wonder if I just change that 7 to a 1?”

At that point, I installed PowerShell on a test machine and copied the folder structure that I had to purge. I then made a second copy because the network was slow that day, and I didn’t want to waste time.

I edited the folder name to match the folder that I wanted and the file extension. I also changed that 7 to a 1 and crossed my fingers.

$Now=Get-Date

Get-Childitem C:\VendorApp\*.TXT | Where-Object { $_.LastWriteTime –lt $Now.AddDays(-1) }

I blinked and looked. It displayed only files that were older than today and that had the TXT extension. I was floored. To learn how to remove them, I was ready for a long, drawn-out session. “Grumble grumble…I’ll bet VBScript is involved here somewhere.”

To remove the files, I saw only one extra bit of data. Something called Remove-Item seemed a pretty sensible name. I appended the last bit of code and read a bit further as the article described how to test it without doing anything dangerous by using the whatif parameter.

I shook my head and muttered something about “Pigs flying before this would work,” but I tried it anyway.

$Now=Get-Date

Get-Childitem C:\VendorApp\*.TXT | Where-Object { $_.LastWriteTime –lt $Now.AddDays(-1) } | Remove-Item –whatif

Staring and blinking at the screen of output, I scrolled up. It looked like something happened to the data. But, upon examining the folder, nothing happened. It really did a “What if I do this?”

To actually do the damage, it asked me to remove the –whatif and run the cmdlet.

$Now=Get-Date

Get-Childitem C:\VendorApp\*.TXT | Where-Object { $_.LastWriteTime –lt $Now.AddDays(-1) } | Remove-Item

“Wow!” Staring at the folder, I was speechless. Only 24 hours files left of files ending in TXT.

I had been on site for only 10 minutes, and the problem was solved….almost.

“I’ll bet this won’t work on the server,” I thought. I was ready for the worst. This was my first time using PowerShell and I was untrusting.

So, repeat process, install PowerShell 1.0 on the Windows Server 2003 R2 environment, copy this script, duplicate the folder of data. After all, I wasn’t about to see how good their backup system was.

I tested the process, and the results were the same. Now I had to figure out how to schedule this. Oh, this was like DOS. Instead of running cmd.exe or command.com, it was PowerShell.exe.

I made a little folder called “C:\DataClean”, saved this new script called LogClean.PS1, and set up a scheduled task.

PowerShell.exe –executionpolicy Bypass –file C:\DataClean\LogClean.PS1

I ran the task and crossed my fingers.

POOF! It all just worked!

I was on site for barely 20 minutes for a two-hour minimum billable call. I was also on the road early and beat rush hour traffic on a Friday.

Hearing AC/DC’s Highway to Hell on the radio and with the knowledge of what technology got me home early that day, I began singing loudly all the way home. Singing to the chorus of the song, the words “I’m using PowerShell! I’m using PowerShell!” kept bursting from my lips.

Yes, I love using PowerShell. Over the next while, I’ll try to relate some things that you can use PowerShell for without needing to learn how to script.  Even if you, too, are that field tech with no time on your hands.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow.

Until then, always remember that with Great PowerShell comes Great Responsibility.

Sean Kearney Honorary Scripting Guy Cloud and Datacenter Management MVP

0 comments

Discussion is closed.

Feedback usabilla icon