Wednesday, August 02, 2006

PowerShell Baby Step: Change File Attributes 

I really think there is a lot of potential wrapped up in PowerShell, but since I have rarely used Linux and rarely write batch files, I have hard time getting my head around how to use PowerShell.

I do know that I hate the limitations of batch files and have thought about using Ruby and now PowerShell instead. I installed ShinyPower to help me navigate the PowerShell help. I read some tutorials. Gradually, I was getting ready to do something.

Well, today I finally wrote my first PowerShell code and this is why.

I use a fantastic program called TimeSnapper. It saves an image of my screen at a pre-determined interval--in my case every 5 minutes. This helps me keep track of what I have been working on. Well, I copied the image files to a new location and the CreateDate changed. This meant that TimeSnapper thought all the images were captured at the same time each day. Ugh. Then I thought, "this sounds like a job for PowerShell"

After a little exploration I came up with a script to fix the problem with the files.

ls | foreach { $_.CreationTime = $_.LastWriteTime }

Breakdown:
Step1 [ ls ] Call the ls alias to list the files in a given directory.
Step2 [ | ] Pipe the resulting list of FileObjectInfo objects to a for each statement
Step3 [ foreach { ] Begin the for each
Step4 [ $_ ] Reference the current object in the collection
Step5 [ .CreationTime = $_.LastWriteTime ] Set the CreationTime equal to the LastWriteTime
Step6 [ } ] End the for each

In the end, it's a pretty simple one-line script, but it helped me make some progress understanding the PowerShell syntax.

Here's some other cool PowerShell tricks. "Can your programming language do this?"

This page is powered by Blogger. Isn't yours?