So today I began my first delve into (what has turned out to be) the incredibly powerful Powershell scripting environment. The need to automate some repetitive tasks whilst limited to a Windows OS initially seemed like an impossible task. Not wanting to hack together some awkward batch files, I decided, as I am currently using Windows 7 as my full-time Windows OS to use the ‘included by default’ Powershell – and such a good decision that turned out to be!
What started out quite confusing, quickly became much more familiar as my logic and limited knowledge of programming slowly came back to me. Now, I am by no means a seasoned coder, I would even go so far as to say I am a complete n00b when it comes to programming, however I have a knack for lateral thinking and a limited knowledge of scripting (BASH scripting mainly). With this knowledge I began to hack away at the keyboard, slowly carving out what turned out to be a quite flexible, and functional code.
I had a few important functions that I needed to script, and anything additional to that I considered a bonus. The basic idea was to take some parameters, and create a file/folder structure based around those parameters, allowing various files to be moved in/out of the folder structure & actions performed upon those files. The variables I needed were as follows:
- $name
- $month_year
- $date
The above variables were the bare minimum for what I wanted to do. The idea was to automatically create a folder structure in the following format – $name > $month_year > $date. However, I must also check to see if any part of the structure already exists. This turned out to be fairly simple after some research and experimentation with various code. Eventually settling on a nice nested if block (the syntax & variables will make more sense if you read the entire code listed below):
Once the folder structure had been checked/created a file (zipped & password protected) was to be copied from a usb flash drive to the current dates folder, unzipped and the files sorted. This threw up a number of issues, such as how programs (7zip in this case) are referenced from within Powershell, whilst allowing command line switches based on variables being passed to the external program.
Please also note my use of function calls within the script, which again shows the available power (no pun intended) of the environment.
The entire script is shown below. All in all I think this script, considering I have never used Powershell, or have any background programming experience, performs pretty well. Now I’m sure there is probably much that could be done to streamline the code and suggestions are more than welcome.



I’m looking forward to seeing the script – is that coming?
Experiment! Enjoy! Engage!
Jeffrey Snover [MSFT]
Distinguished Engineer
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
Hi Jeffrey,
I should upload the script tomorrow, I just need to paste-bin it from work, I have a hard copy but I’m not likely to handwrite it all again! Retaining the script format in WP seems to be a little hit and miss (see my WP html editor post). Thanks for taking an interest, hopefully you can spot all the inefficiencies & help me out
Matt