Hey, Scripting Guy! How can I use Windows PowerShell to locate and delete all the .tmp files on a drive?
— OR
Hey, OR. You know, there’s an old saying: Give a man a fish and he’ll eat for a day; teach that man to fish and he’ll eat for the rest of his life.
Well, unless he’s like the Scripting Guy who writes this column and doesn’t like seafood. In that case he probably won’t even want to learn to fish. And there’s no point in giving him a fish, because he won’t eat it. Instead, he’ll end up starving to death and you’ll spend the rest of your life wracked by guilt. “Why did I give him a fish?” you’ll think. “Why didn’t I give him a hamburger or something? This is all my fault, isn’t it?”
Yes, it is.
The point is – well, to tell you the truth, we’re no longer sure what the point is. Oh, now we remember. Can we show you how to use Windows PowerShell to locate and delete all the .tmp files on a drive? Of course we can. But then you’d run a script for just one day. But if you learned Windows PowerShell then you could run scripts for the rest of your life. (You’ll still be hungry, but at least you’ll have something to take your mind off food.)
In other words, if you’re interested in Windows PowerShell then you owe it to yourself to attend Windows PowerShell Week, November 6-10, 2006. This week is designed with one purpose in mind: to introduce you to Windows PowerShell. We’ll have a series of webcasts (one each day, Monday through Friday); we’ll have a virtual lab where you can try your hand at Windows PowerShell; and we’ll have a few other assorted goodies (like a “labcast,” which is a combination virtual lab and webcast). It will be fun, it will be exciting, and, with any luck, it will be educational as well. We won’t teach you how to fish, but we will teach you everything you need in order to get started with Windows PowerShell.
Well, that’s a good point: you never know what Peter will do during a webcast, do you? So maybe Peter will teach you to fish; we’ll just have to wait and see.
Update: Windows PowerShell Week is over, but you can watch the webcasts on-demand anytime. |
So, other than fishing, what kinds of things will you learn during Windows PowerShell Week? Why, things like how to locate and delete all the .tmp files on a drive:
get-childitem c:\ -include *.tmp -recurse | foreach ($_) {remove-item $_.fullname}
Believe it or not that is the entire command; as you can see, if you don’t enjoy typing then Windows PowerShell is like a dream come true. What we have here are actually two commands crammed into one: we first retrieve a collection of all the .tmp files on drive C, then we go ahead and delete each of those files. How do we do that? Let’s see if we can figure that out.
To begin with, we use the Get-ChildItem Cmdlet to retrieve a collection of all the .tmp files on drive C. That’s what we do here:
get-childitem c:\ -include *.tmp -recurse
This is actually fairly straightforward. We call Get-ChildItem (which, when working with the file system, functions somewhat similar to the dir command) and pass it three parameters:
• |
c:\, which represents the starting folder for our search. |
• |
-include *.tmp, which tells Get-ChildItem to return only files that have a .tmp file extension. Suppose we also wanted to include files that have a .temp file extension. That’s fine; all we’d have to do is add .temp to the list of included files: -include *.tmp, *.temp. |
• |
-recurse, which tells Get-ChildItem to look for files in all of the subfolders of C:\ (and all the sub-subfolders of those subfolders, and so on). And yes, that is cool: you don’t have to write some complicated recursive function to get at the files in the subfolders of a folder. Instead, just tack on the –recurse parameter. |
After we’ve retrieved our collection we “pipe” it over to the second half of our command (the | character represents the pipeline). Needless to say, we’ll talk about piping in much more detail during Windows PowerShell Week. For now we’ll just say that, by itself, Get-ChildItem typically retrieves a collection and then displays the returned information onscreen. However, we aren’t interested in seeing the .tmp files displayed on screen; we want to delete those files. Therefore, instead of displaying the items on screen we ask Get-ChildItem to hand over (pipe) the entire collection to the second half of our command.
In case you’ve forgotten, the second half of our command looks like this:
foreach ($_) {remove-item $_.fullname}
What we’re doing here is setting up a For Each loop to loop through the collection handed over by Get-ChildItem. Don’t worry too much about the syntax for now; we’ll discuss For Each loops in much more detail during Windows PowerShell Week. We will note, however, that the $_ is a special Windows PowerShell variable that represents the individual items in the pipeline. When we say foreach ($_) that’s equivalent to a VBScript statement like this:
For Each objItem in colItems
In other words, we’re just looping through all the files in the collection.
And what are we going to do to each of those files? Why we’re going to use the Remove-Item Cmdlet to delete them, of course:
{remove-item $_.fullname}
Here we call Remove-Item and pass, as the sole parameter, the FullName property of each individual file in the collection. (FullName is equivalent to the file path.) Believe it or not, that’s all we have to do; Remove-Item takes care of the rest for us.
What’s that? You say this sounds cool, but, seeing as how you’re new to Windows PowerShell, you’re a little nervous about running a script that deletes a bunch of files? Hey, no problem. If that’s the case, just tack the –whatif parameter on to the Remove-Item command, like so:
get-childitem c:\ -include *.tmp -recurse | foreach ($_) {remove-item $_.fullname -whatif}
That –whatif parameter is pretty neat: instead of deleting the .tmp files it simply tells you what would happen if you really did ask Remove-Item to delete all the files. In other words, you’ll get back a report of the files that would be deleted if you ran the command for real:
What if: Performing operation “Remove File” on Target “C:\WINDOWS\SET3.tmp”. What if: Performing operation “Remove File” on Target “C:\WINDOWS\SET4.tmp”. What if: Performing operation “Remove File” on Target “C:\WINDOWS\SET8.tmp”.
It’s like fantasy football, only with Windows PowerShell.
At any rate, we – oops, we need to go: Peter just walked by wearing hip waders and carrying a fishing pole. Admittedly, that’s not all that unusual for Peter. But we better find out what he’s up to, just in case.
0 comments