Wednesday, March 05, 2014

Windows PowerShell Script to Delete Old Files

Just starting to dab into PowerShell script after it has been in the wild for 8 years. Turns out to be surprisingly good. First you have a nice editor (PowerShell ISE), second is the vast amount of cmdlets that make coding go fast.

Here is a script to delete old files that are 30 days or older. It used to take at least 40 lines of VBS to achieve.

Before you get too excited, a few things to be aware of:
  • Availability of PowerShell: WindowsXP may not have it, and Windows 7 may only have it up to version 2. So, if you found some exciting cmdlets, chances are they are not available on your OS by default
  • Executing a PowerShell file takes extra effort. It's forbidden by default. So you have to do either of the following:
    • Open PowerShell, and execute this command: 
         Set-ExecutionPolicy RemoteSigned
    • Or, when running a file use this command line (notice full path to the ps1 file has to be provided here): 
         PowerShell.exe -ExecutionPolicy ByPass c:\scripts\myScript.ps1


No comments: