Ezine 140 - PowerShell Script to Delete Temp FilesPowerShell Script to Delete Temp FilesI sense a changing of the guard - out with VBScript, and in with PowerShell. To date, my best work has been persuading over 2,000 people to download PowerShell from Microsoft's site. However, downloading is not the same as using in a production network. Therefore, my new project is to provide a bank of handy cmdlet scripts; each cmdlet will automate a task in Windows Server 2003 / XP / Longhorn / Vista, and thus turn dabblers into believers. This week we tackle deleting temp files. Topics - Delete Temp Files Using PowerShell
♣ This Week's SecretI am disappointed because over the last 3 months I have spent little time with PowerShell. Also, I am slightly surprised that there has not been more information in the press about PowerShell. One obvious comment is - Guy you have been reading the wrong articles! What I am pleased with is how easy it is to get back in the saddle. It has been straightforward to pick up where I left off in December. The verb-noun pairs, the output pipe (|), and the syntax have all come flooding back. Ezine 139 - Correction / UpdateThe main mission in Ezine 139 was to delete temporary files and my Example 2 worked a treat. The problem was with Example 1, which merely listed files. I only tested it with my OnScript editor, thus I overlooked that if I ran the script as suggested, it created about 97 message boxes, each with the name of one temporary file. Apologies. I have now amended the code in the online copy of Ezine 139, as a result the script gives just one message box. Thanks to Dan Weiner to alerting me to the problem. This week's missionThis Week's Mission is once again to delete temporary files. However, the twist is we will use PowerShell rather than VBScript. It will be fascinating to compare how the old and the new scripting languages delete the temporary files. Trap Preparation Download PowerShell from Microsoft's site. One interesting point is that there are different versions of PowerShell for XP, Windows Server 2003 and Vista. Scripting Environmental VariablesTo make sense of this script you must understand Environmental Variables. Here is how you can check
your operating system's temp and windir variables Now link what happens when you use these %variables%, with what you see in the System Icon, Advanced, Environmental Variables. Note in passing that Temp and Tmp both occur under the User and System variables. Also be aware that the User's %temp%, which we will use in our script, corresponds to %USERPROFILE%\Local Settings\Temp. As you may know, in XP the %USERPROFILE% is stored under the Documents and Settings folder. If you are looking for handy network utilities, try some of the free downloads at Tools4Ever Example 1: PowerShell Script to List the Temp FilesTo gain confidence, and for the sake of safety, I want start by just listing the temporary files. Instructions Method 1 - Copy and paste the code below into a PowerShell command box. Method 2 (Better) - Copy the code below into a text file. Save with .ps1 extension e.g. C:\scripts\List.ps1.
cls Note 1: It is hard to believe, but the most difficult part of this project was to research the correct syntax for : $Env:temp. Note 2: Set-location is much like CD (Change Directory) Note 3: get-childitem, together with -recurse is like the dos /s switch. Note 4: Where-object is just a safety clause to make sure that we only list .tmp files. Note 5: The key to PowerShell's loop syntax is to examine two type of bracket (condition) {Do Stuff} Example 2: List Temp Files --> Output to a FileAfter last week's debacle, here is extra code, which outputs the list to a file. The key point is to pipe (|) the result to out-file. Instructions, see and modify those is Example 1. cls Note 1: I hope that you changed this line: $File = "C:\ListTemp.txt". It is bad practice to save such files to the root, my problem is that I do not know if you have a c:\scripts folder, so I dare not save to a non-existent folder. Note 2: `t is PowerShell's tab command; the equivalent of VbTab. Guy Recommends:
The Free IP Address Tracker (IPAT)
| |||||
Custom Search
|
Guy Recommends: WMI Monitor and It's Free!
|
|
Home Copyright © 1999-2012 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |