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”
let create a new folder named “Backup_A”
Now we have two folders “Folder A” and “Backup_A”
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.
:::: 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
:::: 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.
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.
:::: 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
BUTTTTTTTT !!! wait…!!!!!!!!!!!!!
Check the folder again, Recurse_Testing have some Sub-folders which are not copied in backup_A
-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
let check if it copied subfolder also.
Seems Identical isn’t.
:::: 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
lets check if these two folders are identical.
Seems like TWINS isn't ;-)
Hope it is useful to someone .
Thanks
Aman Dhally
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.
ReplyDeleteGood effort again..
Hi Puneet,
DeleteThanks 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
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.
DeleteI 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.
Hi Puneet,
DeleteYes 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
THis is great i had a project that i needed to complete and this really helped thank you
ReplyDelete