Home > Powershell > Copy Folder Contents Using Powershell

Copy Folder Contents Using Powershell

I’m someone who has always believed that we should live our lives by example. We are defined by our actions not our words. I recently found a decorative art piece that I’ve hung above my desk that says the following: Excellence is not an act…It’s a habit….Oh how true this is. Very few us have such natural abilities that would, without cultivation, contribute to an immediate rise in excellence. I, my friends…am no exception. In my quest to not just learn Powershell, but master it, I have driven an already overly compulsive individual..myself.. to find any opportunity to…well…Powershell…

In my experience the best way to learn something is through constant repetition. My compulsion begins each morning where I spend at least the first half hour of my day dedicated to the memorization of a several scripts. I write them over and over until they are committed to the very short term memory I have left at my disposal. Not sure what old memories are sacrificed during this act but if I couldn’t remember them anyway so be it. These scripts are rarely of my own creation and are part of a greater collection that I feel would contribute to a balanced repository of scripting knowledge. Learning one often helps in learning others as the constructs don’t change, only what info they are trying to reveal or manipulate.

One script I’ve committed to memory is used to look through files and folders on an FTP server and then delete any items that are older than 30 days. The deleted items are, of course, first stored in a variable and then written to a log file for further review. I have a folder which I use for testing the script and I’m always copying the contents from a backup folder into the one to be deleted each time the script is to be run. And so the epiphany was …what if I could design a script that compares the two folders and then does this for me? That way I can spare myself the boring and mediocre task of doing this manually. The challenge therefore has been laid down…Let’s see if I can put what little experience I have to good use..

The new script must do the following:

  1. Define the folders I want to compare
  2. If the first one is empty then it copies the contents from the second
  3. If the first one is not empty perform the copy anyway and suppress any error msgs.

Seems simple enough…

So let’s come up with a list of cmdlet’s that will help do the job. Although I’ve working with the final choosen list of cmdlet’s I still used the trusty old “get-command –noun” syntax to query if there were any additional cmdlet’s that could be of use. Once the cmdlet is found we can get info on it’s use by using ” help compare-object –full”

The code required the following cmdlets and contructs:

  • Compare-object
  • Copy-Item
  • Foreach-Object
  • Write-Host
  • If\Else (construct)

Below is the final script which I’m sure will be improved upon in the days to come.

Categories: Powershell
  1. January 24, 2012 at 6:45 pm

    This is a good script. I plan on using this and expanding upon it for another daily task.

  1. January 24, 2012 at 7:24 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: