Curly Blue and the meaning of scripting – Part 1

Summary: Curly Blue learns how to automate cleaning of old data files by using PowerShell.

This week, in the spirit of the holidays (and of course the previous six years!), Hey, Scripting Guy is honored to bring forth a repeat of a tradition: The “Hey, Scripting Guy!” holiday special.

This year, we are pleased to offer a week of fun and Windows PowerShell with a new story produced as a collaboration of two MVPs, Honorary Scripting Guys, Thomas Rayner and Sean Kearney.

This year’s special is about a fellow named Curly Blue who has just started his new job with only a week remaining before the holiday weekend. Curly Blue has never needed to learn scripting or PowerShell, strangely enough, mostly due to his hyper typing skills. He knew so many keyboard shortcuts in Windows that he was known as one of the fastest administrators in the West.

In his new job, Curly Blue has a series of challenges that make him question his skills and abilities. He’s heard about PowerShell but dismissed it as “Yet another one of those scripting languages….” And, since he was so fast in the GUI with his shortcuts, he never discovered the meaning of using it.

We now enter the office of Curly Blue at Contoso Ornaments. There’s a knock at the door. It is his co-worker “Linux”, nicknamed due to his expert ability to automate every Unix environment in Contoso. As the new Windows admin and a perceived automation champ in Windows, somehow fate has decided that Linux and Curly will be the best of buddies (or cool arch-rivals, like Batman and the Joker…. but we’ve decided they will be friends versus fiends in the spirit of the holidays).

By the way, at Contoso, most of Curly Blue’s coworkers tend to go by Cool handles and nicknames. It is a pretty relaxed environment (and this will be our excuse for all the strange names you will hear for employees).

Linux looked up at Curly Blue, “All settled in? Got enough Perrier in your fridge? Bean bag comfy enough? Has your new Surface Book 4 arrived yet?”

Curly Blue smiled. “Yup.  All is here and quite good. So, what’s on the burner today, Linux?”

Linux handed Curly the first change request document. “This shouldn’t be a challenge, but we need to implement this pretty quickly. We have a vendor server in production right now, but the challenge is that every night it shuts down due to a lack of free space.”

Curly looks up at Linux. “Free space? In the modern day of terabytes of storage? Surely, you’re kidding.”

“I’m not… and please don’t call me Shirley. No, the problem is pretty basic. The vendor did not mark in any type of maintenance in the log module. There will be a patch later, but until it comes out, we need to get rid of files in the log folder that are older than 24 hours.”

Curly was a bit nervous. It needed to be automated. Normally, he would execute a remote desktop session with a quick work of some keystrokes. He didn’t want to get fired and decided to confide in his new friend, Linux.

“I’ve never really discovered the true meaning of scripting. I know it’s there, but, in one of my past jobs, I did data entry and could do almost 25,000 keystrokes per hour. I used to burn out keyboards. I never had the time to learn,”

Linux could see his new friend was nervous about losing his job. “Curly Blue, no need to fret. I can show you how to do this with PowerShell.”

Linux spent a few minutes showing Curly Blue a quick “one-liner” in PowerShell.

“Have you ever used the command prompt at all?”

Curly nodded. “From time to time, I’ve used it to like delete files and work in the directory.”

Linux showed him an example of how to get the directory in PowerShell. “So, to get a listing of all files that have the .LOG extension, you might have typed this?”

DIR C:\LogDir\*.LOG

He looked up. “Yes, that looks right.”

His new friend continued. “In Windows PowerShell, this is an ‘Alias’, which is really doing this.”

Get-ChildItem C:\LogDir\*.LOG

“In actual fact, there are several ways to do this in PowerShell. What I can do in PowerShell is ‘pipe’ or send that information to a receiving cmdlet like Remove-Item.”

Curly had heard of piping. He had seen some occasionally at the command prompt when parsing large log files with the MORE command.

Linux continued. “One advantage of PowerShell is that it’s not only easy use, but it treats all data as objects.  In the file system in Windows, you’ll note how Explorer shows not only the file name but also the date it was modified and even the file type and size, correct?”

List of files displayed in Windows Explorer

He nodded in agreement. “Yes, but when I did this in the command prompt, it was just more text to parse.”

Linux showed him a Get-Member of that same folder. “All of this is information we can access and consume in Windows PowerShell. I can access these as properties from the object.”

Result of Get-Member for a folder

“I can also grab the current date with the Get-Date cmdlet and use it to compare against the LastWriteTime of a file.” He typed a quick example.


Get-ChildItem C:\LogDir\*.LOG | Where-Object { $_.LastWriteTime -lt $Today }

“Now what this would do is show me all the files that were created before this exact time. Now, unless you had a T.A.R.D.I.S., most of these files were created earlier. But, we can step forward in time with a method called adddays in PowerShell. In this example, I can show all files that are older than seven days.”

Get-ChildItem C:\LogDir\*.LOG | Where-Object { $_.LastWriteTime.adddays(7) -lt $Today }

Curly’s eyes lit up. “So, if we changed that seven to one, would that show us files from the past 24 hours?”

Linux nodded. “Now, all we need to do is to pipe this result to Remove-Item. I should also mention that most cmdlets in PowerShell that are destructive in nature have a -whatif switch. This allows us to test things live to make sure the format is at least correct.”

Curly Blue watched as Linux entered the PowerShell line on the development version of the vendor server.

Get-ChildItem C:\LogDir\*.LOG | Where-Object { $_.LastWriteTime.adddays(7) -lt $Today } | Remove-Item -whatif

Curly watched the output.

Result of Remove-Item

Linux then removed the -whatif switch, which immediately purged the files. “We can now save this as a file called CLEANUP.PS1 and use the task scheduler to schedule the task in the following manner in our development system.”

PowerShell.exe -executionpolicy Bypass -file .\Cleanup.ps1

Curly Blue looked over at Linux. “I get it, the nature of scripting is to make things repeatable, but I’ll have to learn PowerShell first to make this useful. For all of that effort, I could have just visually selected the files and erased them myself, even if I had to VPN in to make it happen.”

Linux looked over at his friend. “But, could you do it in a fashion that was consistent and could be validated, which is what we just did was in the development environment? We can now just pass this onto QA and have them verify our work before promoting to production. Although this was a simple task, this can be repeated across the farm of the vendor’s servers.”

Curly Blue saw his point. “Come on down the hall, I’ll introduce you some of the rest of the staff.”

Pop by for a visit tomorrow as our good friend, Curly Blue, discovers more about the meaning of scripting.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at, 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


Comments (0)

Skip to main content