# CopyTo() Coppying form one array to another.

**URL:** <https://forums.powershell.org/t/copyto-coppying-form-one-array-to-another/15605>\
**Category:** PowerShell Help\
**Created:** [February 9, 2021, 6:44am UTC](https://forums.powershell.org/t/copyto-coppying-form-one-array-to-another/15605 "2021-02-09T06:44:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunny123](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sunny123](https://forums.powershell.org/u/sunny123)\
**Post date:** [February 9, 2021, 6:44am UTC](https://forums.powershell.org/t/copyto-coppying-form-one-array-to-another/15605/1 "2021-02-09T06:44:25Z")

</div>

Hi

I’m Trying to Copy content of one file to another file using the copy() Method.

File 1 this is the file I want copy :

$folder2 =@(“C:\Users\spate\Documents\SampleFile10.txt”)

To

$path2 = @(“C:\Users\spate\Documents\SampleFile17.txt”)

&nbsp;

Doing this and getting error:

$folder2.CopyTo(“C:\Users\spate\Documents\SampleFile10.txt” + $path2)

Thanks

Sunny!

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [February 9, 2021, 7:00am UTC](https://forums.powershell.org/t/copyto-coppying-form-one-array-to-another/15605/2 "2021-02-09T07:00:17Z")

</div>

You cannot change the size of a standard array in PowerShell. You can instantiate a new array by using the + operator to do what you are trying to do with the copyto method.

```
$folder2 =@(“C:\Users\spate\Documents\SampleFile10.txt”)
$path2 = @(“C:\Users\spate\Documents\SampleFile17.txt”)

$folder2 + $path2
C:\Users\spate\Documents\SampleFile10.txt
C:\Users\spate\Documents\SampleFile17.txt
```

Having said that in your post you said, [quote quote=291502]Copy content of one file to another file[/quote]  
None of this has to do with the content of a file only an array of strings containing a PATH to a file NOT it’s contents. To do that you will need to use some cmdlets let Get-Content and Add-Content

```
$folder2 =“C:\Users\spate\Documents\SampleFile10.txt”
$path2 = “C:\Users\spate\Documents\SampleFile17.txt”

Add-Content -Path $path2 -Value (Get-Content -Path $folder2)
```

See Get-Help Add-Content and Get-Help about\_arrays

---

<div class="post-metadata">

**Author:** ![james\_yumnam](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@james\_yumnam](https://forums.powershell.org/u/james_yumnam)\
**Post date:** [February 9, 2021, 7:31am UTC](https://forums.powershell.org/t/copyto-coppying-form-one-array-to-another/15605/3 "2021-02-09T07:31:39Z")

</div>

There are many easier ways you can copy the contents of a file to a another file in PS.

But if you are really into using **Copyto()**, you should know that this function copies the content of an existing file to a new file with same attributes and it does not allow overwriting, which means that you need to delete any existing files with the same name as the destination file to be used in the CopyTo().

&nbsp;

Below code should work using CopyTo()

```
$file1path = "D:\powershell org\file1.txt"
$file1info = [System.io.FileInfo]($file1path);
$file2path = "D:\powershell org\file2.txt"
$file2info = [System.io.FileInfo]($file2path);
if(Test-Path $file2path)
{
Remove-Item $file2path -Force
}
$file1info.CopyTo($file2info)
```

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![sunny123](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sunny123](https://forums.powershell.org/u/sunny123)\
**Post date:** [February 9, 2021, 7:36am UTC](https://forums.powershell.org/t/copyto-coppying-form-one-array-to-another/15605/4 "2021-02-09T07:36:16Z")

</div>

Hi Mike

I’m knew to powershell scripting. The second script you have shown will definetly do the trick.

My whole point of doing the way I tried was to understand how i can encorperate the copy() method to do the same.

Basically understand

CopyTo Method void CopyTo(array array, int index), void CopyTo(array array, long index), void ICollection.CopyTo(array arr…

Thanks

Sunny

---

<div class="post-metadata">

**Author:** ![sunny123](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sunny123](https://forums.powershell.org/u/sunny123)\
**Post date:** [February 9, 2021, 7:49am UTC](https://forums.powershell.org/t/copyto-coppying-form-one-array-to-another/15605/5 "2021-02-09T07:49:41Z")

</div>

Hey James

I can follow your logic and understand the the flow/sructure of the script. This really helps.

But What you are doing in this step is confusing for me.

Are you initiating a array here?

$file1info = [System.io.FileInfo]($file1path);

Thanks

Sunny

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![james\_yumnam](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@james\_yumnam](https://forums.powershell.org/u/james_yumnam)\
**Post date:** [February 9, 2021, 8:02am UTC](https://forums.powershell.org/t/copyto-coppying-form-one-array-to-another/15605/6 "2021-02-09T08:02:44Z")

</div>

Hi Sunny,

No, I am not initializing an error here. I am creating a **FileInfo** Object for your source/destination files. A FileInfo object provides properties and instance methods for the creation, copying, deletion, moving, and opening of files.

---

<div class="post-metadata">

**Author:** ![sunny123](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sunny123](https://forums.powershell.org/u/sunny123)\
**Post date:** [February 9, 2021, 9:07am UTC](https://forums.powershell.org/t/copyto-coppying-form-one-array-to-another/15605/7 "2021-02-09T09:07:13Z")

</div>

GOT IT TY

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:27pm UTC](https://forums.powershell.org/t/copyto-coppying-form-one-array-to-another/15605/8 "2024-05-16T20:27:54Z")

</div>

Closed on May 16, 2024, 8:27pm UTC


