Thread: Copy-Item
View Single Post
Old 05-03-2007   #5 (permalink)
IJuan


 
 

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