Apple iTunes
 

I’ve got a real quick tip for today to demonstrate how to filter a file list using Windows PowerShell.

Sample ONE:

Here I want to list all *.log files that were created before today:

PS C:\>get-childitem *.log |? {$_.creationtime -lt $(get-date).adddays(-1)}

Note, you can replace “get-childitem” with a provided shortened alias such as “gci” or “dir” (that’s what I do anyway)

Sample TWO:

Here I will take the list from above and delete the files that are found:

PS C:\>get-childitem *.log |? {$_.creationtime -lt $(get-date).adddays(-1)} |% {remove-item $_}

Note: One of the main reasons we like to work from the PowerShell command-line is to get work done faster, right? To that end, I very rarely enter the full cmdlet names when I’m working on something and quite often find myself using the DOS-like aliases (although that’s probably not such a good habit).

Here’s Sample TWO again with aliases:

PS C:\>gci *.log |? {$_.creationtime -lt $(get-date).adddays(-1)} |% {del $_}

Sample THREE:

Here I will find all zero-length LOG files and delete them:

PS C:\>gci *.log |? {$_.length -eq 0} |% {del $_}

 

That’s all for now, hope this help someone to make better use of Windows PowerShell!

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
© 2011 Posh Tips Suffusion theme by Sayontan Sinha

Page optimized by WP Minify WordPress Plugin