Overwrite Destination With Powershell
-
Friday, November 30, 2012 8:19 PM
I am a novice PowerShell user but I found a short command to copy one file to another location:
[System.IO.File]::Copy("I:\All\Applications\Schedule\directoryname\filename.xls","C:\Users\username\directoryname\Documents\Cabot S\filename.xls");It works fine but only for the initial copy. I need it to overwrite each time we run the script. I have made an exhaustive search for this answer and oddly enough, I cannot find the correct syntax I need to do this.
Suggestions?
Thanks!
Brian
All Replies
-
Friday, November 30, 2012 8:27 PMModerator
Why use the IO.File Copy method when you can just use the copy-item cmdlet?
Bill
-
Friday, November 30, 2012 8:29 PM
I also tried this:
Copy-Item -force("I:\All\Applications\Schedule\directoryname\filename.xls","C:\Users\username\directoryname\Documents\Cabot S\filename.xls"); and I didn't get an error but the destination didn't seem to change and if I deleted the file at the destination, this script will not copy the file at all. -
Friday, November 30, 2012 8:29 PM
There is a File.Copy() overload that allows you to specify if you want to overwrite the destination file if it exists:
File.Copy Method (String, String, Boolean)
But you should look into the Copy-Item cmdlet first:
-
Friday, November 30, 2012 8:32 PM
Why use the IO.File Copy method when you can just use the copy-item cmdlet?
Bill
I am a novice and it was the first command I found. -
Friday, November 30, 2012 8:36 PM
There is a File.Copy() overload that allows you to specify if you want to overwrite the destination file if it exists:
File.Copy Method (String, String, Boolean)
But you should look into the Copy-Item cmdlet first:
Sorry...being a novice, that is all greek to me...I thought it would be easy to copy a file and overwrite it with a PowerShell command. I looked over that second article and nothing made enough simple sense to help me overwrite the destination. -
Friday, November 30, 2012 8:53 PMModerator
Try this:
Copy-Item -path 'I:\All\Applications\Schedule\directoryname\filename.xls' -Destination 'C:\Users\username\directoryname\Documents\Cabot S\filename.xls' -Force
Also see
get-help copy-item -full
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
- Proposed As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Friday, November 30, 2012 9:32 PM
- Marked As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Monday, December 31, 2012 5:24 PM
-
Friday, November 30, 2012 8:55 PM
OK, have a look at this article:
-
Friday, November 30, 2012 9:39 PMThanks for the nudges...going to try the test-path remove-item commands and see if it helps.
-
Friday, November 30, 2012 9:42 PMModerator
Note that copy-item will overwrite the destination without prompting by default. The only problem you may run into is if the destination file is read-only (-force will take care of that), or if the file is open, in which case the only remedy is to close the file first, then overwrite it.
Bill
- Proposed As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Monday, December 03, 2012 5:15 PM
- Marked As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Monday, December 31, 2012 5:24 PM

