Pages

Friday, November 18, 2011

Copy Files and Folders using PowerShell

 

 

Hi,

Copy files and folders this is the task which every Admin or even a end user performs daily.

How we can Copy files and folder using PowerShell?

Quite Easy, we just need to use Copy-Item cmdlet with few Switch Parameters that all.

Cmdlet used : Copy-Item

:::: Copy Files

Lets create a a new folder to Backup files currently I have  one Folder named “Folder_A” which contain Data and we will create a new folder named as “Backup_A” , and then copy data from “Folder_A” to “backup_A”

Files-1

let create a new folder named “Backup_A”

Files-2

Now we have two folders “Folder A” and “Backup_A”

Files-3

now copy a file from “Folder_A” to “backup_A” ,

command is: Copy-Item D:\Demo\Folder_A\Book.txt -Destination D:\Demo\Backup_A

file successfully copied.

Files-4

:::: Copy Txt file only

-Include

if we want to copy specific type of files only then we can use –Include switch Parameter followed by the type .

-Verbose 

We can use –Verbose switch to see which files are getting copied

command: Copy-Item D:\Demo\Folder_A\* -Destination D:\Demo\Backup_A -Include *.txt –Verbose

see the screenshot, command copied the .Txt files only

Files-5

:::: Exclude files to copying

-Exclude

If we are using Copy-Item cmdlet in a backup script it would be good we don’t backup Music,Video files on server. As a Ideal Policy we should not fill our SAN, Server storage with the backup of users music and videos.

to exclude these files to be copied use –Exclude switch parameter followed by file types example *.mp3,*.mp4 etc

in my Folder_A i have few MP3 audio and few MP4 video files, lets stop them copying to our Backup_A folder.

Files-6

note: * means all

Command: Copy-Item D:\Demo\Folder_A\* -Destination D:\Demo\Backup_A -Exclude *.mp3,*.mp4

When i search for *mp3 and  *mp4 no result were found and you can also see there is not a single MP3 and MP4 in “Backup_A” folder.

Files-7

:::: Copy Folders

the command is same like copying file but rather then specify file we need to specify folder

Command : Copy-Item D:\Demo\Folder_A\Recurse_Testing -Destination D:\Demo\Backup_A\

Folder Copied

Files-8

BUTTTTTTTT !!! wait…!!!!!!!!!!!!!

Check the folder again, Recurse_Testing have some Sub-folders which are not copied in backup_A

Files-9

-Recurse

to Copy All folder including sub-folders use –Recurse while copying

Command is:

Copy-Item -Recurse D:\Demo\Folder_A\Recurse_Testing -Destination D:\Demo\Backup_A\  -Verbose

Files-10

let check if it copied subfolder also.

Seems Identical isn’t.

Files-11

:::: Copy all files and Folders

To copy all file and folder we use * wildcard which means all , and –Recurse to copy sub-directories also and we also use –force do that if there any file exists with same name on backup folder it should get overwrites.

command : Copy-Item -Recurse D:\Demo\Folder_A\* -Destination D:\Demo\Backup_A -Force –Verbose

Files-12

lets check if these two folders are identical.

Seems like TWINS isn't ;-)

Files-13

Hope it is useful to someone .

Thanks

Aman Dhally

5 comments:

  1. Good Article. Well written. I think it doesn't list all the switches. You just mentioned those which you think might be cool. If there're others, please mention those switches as well.

    Good effort again..

    ReplyDelete
    Replies
    1. Hi Puneet,
      Thanks for you kind words. Yes this article doesn't include all switches. I used only those switched which were necessary for this article.

      from powershell console type

      Get-Help Copy-Item -online

      This will open a webpage which contain all information about the copy-item cmdlet and their Parameters (switches)

      thanks
      aman

      Delete
    2. I completely forgot about get-Help. That was a simple one. Actually, I am working on a Powershell script to push patches to our servers and I am going everywhere with this. Prior to this,we were using a custom built executable (somewhat similar to pssexec) but we discovered that this won't work as different patches for different versions and processor types need to be pushed and the server names need to be picked up from text file or from the database table, hence we end up rewriting the whole thing in powershell. I need to build something more robust and extensive error handling hence was looking for switches which may help.

      I went through your blog, I must say, you're doing a wonderful work. I am also motivated to start my blog about powershell after looking at yours. :) . Keep up the good work.

      Delete
    3. Hi Puneet,
      Yes we do forget Little Tiny useful things :). Best of luck for your Powershell script.

      I am glad that u liked my blog and it is helpful to you.

      thanks
      aman
      www.amandhally.net

      Delete
  2. THis is great i had a project that i needed to complete and this really helped thank you

    ReplyDelete

Note: Only a member of this blog may post a comment.