Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Copy-Item

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-03-2007   #1 (permalink)
JW
Guest


 

Copy-Item

How do I prevent Copy-Item from overwriting an exsisting file

My System SpecsSystem Spec
Old 05-03-2007   #2 (permalink)
Keith Hill
Guest


 

Re: Copy-Item

"JW" <JW@discussions.microsoft.com> wrote in message
news:3EAB4623-137F-41AD-A4E1-1576431544D5@microsoft.com...
> How do I prevent Copy-Item from overwriting an exsisting file


It shouldn't do that by default unless you specify the -force parameter.

--
Keith

My System SpecsSystem Spec
Old 05-03-2007   #3 (permalink)
JW
Guest


 

Re: Copy-Item

Well on my system it does. According to the documentation the -force switch
only:

"Allows cmdlet to override restrictions such as renaming existing files as
long as security is not compromised"

Which is in my opinion needed when attributes like 'hidden' etc. are
specified.

The command:

Copy-Item C:\test.txt -Destination C:\test\

happily copies test.txt into the C:\test directory clobbering the exsisting
test.txt already present in C:\test

--
JW


"Keith Hill" wrote:

> "JW" <JW@discussions.microsoft.com> wrote in message
> news:3EAB4623-137F-41AD-A4E1-1576431544D5@microsoft.com...
> > How do I prevent Copy-Item from overwriting an exsisting file

>
> It shouldn't do that by default unless you specify the -force parameter.
>
> --
> Keith
>

My System SpecsSystem Spec
Old 05-03-2007   #4 (permalink)
Keith Hill [MVP]
Guest


 

Re: Copy-Item

"JW" <JW@discussions.microsoft.com> wrote in message
news:530C457C-4110-44CC-A556-F1B7B045E2B9@microsoft.com...
> Well on my system it does. According to the documentation the -force
> switch
> only:
>
> "Allows cmdlet to override restrictions such as renaming existing files as
> long as security is not compromised"
>
> Which is in my opinion needed when attributes like 'hidden' etc. are
> specified.
>
> The command:
>
> Copy-Item C:\test.txt -Destination C:\test\
>
> happily copies test.txt into the C:\test directory clobbering the
> exsisting
> test.txt already present in C:\test


Yeah I see the same behavior. It is Move-Item that requires -force to
overwrite a file. Seems a bit inconsistent that Copy-Item doesn't behave
the same way IMO.

--
Keith


My System SpecsSystem Spec
Old 05-03-2007   #5 (permalink)
IJuan
Guest


 

Re: Copy-Item

Here is a quick little function I just made for a work around. If you dot
source it you can use it when ever you need it.

function SafeCopy {
param ([string]$destination = $(throw "Please specify a destination"),
$confirm = $true)
Process {
$fulldestination = (Get-Item $destination).FullName + '\' + $_.Name
if (Test-Path $fulldestination) {
if ($confirm) {
Write-Warning "File exists"
Copy-Item $_.FullName $fulldestination -confirm
}
}
else {Copy-Item $_.FullName $fulldestination}
}
}


I made it to work in the pipeline so that I didn't have support all the
switches that Copy-Item does. Just use it like this:

PS> Get-ChildItem test.txt | SafeCopy c:\temp $false

That way you can use all the switches of Get-ChildItem and even pass it to
Where for further filtering before actually doing your copy.

-= IJuan =-

"JW" wrote:

> Well on my system it does. According to the documentation the -force switch
> only:
>
> "Allows cmdlet to override restrictions such as renaming existing files as
> long as security is not compromised"
>
> Which is in my opinion needed when attributes like 'hidden' etc. are
> specified.
>
> The command:
>
> Copy-Item C:\test.txt -Destination C:\test\
>
> happily copies test.txt into the C:\test directory clobbering the exsisting
> test.txt already present in C:\test
>
> --
> JW
>
>
> "Keith Hill" wrote:
>
> > "JW" <JW@discussions.microsoft.com> wrote in message
> > news:3EAB4623-137F-41AD-A4E1-1576431544D5@microsoft.com...
> > > How do I prevent Copy-Item from overwriting an exsisting file

> >
> > It shouldn't do that by default unless you specify the -force parameter.
> >
> > --
> > Keith
> >

My System SpecsSystem Spec
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can the copy-item cmdlet be used to copy public folders to C: ? JoshGfromPortland PowerShell 8 06-09-2008 09:03 PM
copy-item changing files attributes on network copy failures jas PowerShell 4 01-31-2008 12:39 PM
Getting Copy-Item to display messages (like the old COPY command in CMD.EXE) ?? Marc Scheuner PowerShell 15 10-26-2007 02:41 AM
Copy-Item VRSki PowerShell 4 08-13-2007 10:38 AM
Copy-Item or Copy-ItemProperty and Remote Registry. Brandon Shell PowerShell 1 01-09-2007 10:51 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51