# Copy to Remote PC

**URL:** <https://forums.powershell.org/t/copy-to-remote-pc/24004>\
**Category:** PowerShell Help\
**Created:** [April 25, 2024, 3:13pm UTC](https://forums.powershell.org/t/copy-to-remote-pc/24004 "2024-04-25T15:13:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wilson](https://avatars.discourse-cdn.com/v4/letter/w/96bed5/32.png) [@wilson](https://forums.powershell.org/u/wilson)\
**Post date:** [April 25, 2024, 3:13pm UTC](https://forums.powershell.org/t/copy-to-remote-pc/24004/1 "2024-04-25T15:13:28Z")

</div>

Hi Folks

After got generate my file, I am having troubles to copy to a remote computer, could you help me please?

```auto
get-aduser -filter {accountexpirationdate -ne "$Null"} -Properties accountexpirationdate, name, mail, manager | Select-Object accountexpirationdate, name, mail, manager | export-csv c:\temp\usuarios.txt -NoTypeInformation
$Port = New-PSSession -ComputerName PC1.domain.com
Copy-Item -Path "c:\Temp\Usuarios.txt" -Destination "\\PC1\c$\fotos\Visitantes\" -ToSession $Port

```

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [April 25, 2024, 4:02pm UTC](https://forums.powershell.org/t/copy-to-remote-pc/24004/2 "2024-04-25T16:02:44Z")

</div>

What is the error message?

Also looking at your Copy-Item command, when using the ToSession parameter Destination parameter should be the local path so…

```auto
-Destination "\\PC1\c$\fotos\Visitantes\" 
# should be
-Destination "C:\fotos\Visitantes\"

```

---

<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:** [April 25, 2024, 4:11pm UTC](https://forums.powershell.org/t/copy-to-remote-pc/24004/3 "2024-04-25T16:11:38Z")

</div>

What error message do you get? Can you better describe your goal? Where does the original item actually exist? From your code it’s looking locally on the system you’re running it from, but I have a feeling the original item might be on [PC1.domain.com](http://PC1.domain.com). Nontheless you need to clarify that.

Also take a closer look at the docs:

[Copy-Item (Microsoft.PowerShell.Management) - PowerShell | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-7.4#-tosession)

" Specify the **PSSession** object to which a remote file is being copied. When you use this parameter, the **Destination** parameter refers to the local path on the remote machine."

I don’t think you’re using that cmdlet how you are intending. The way it’s written now won’t work, because the destination should be local path based on the remote system in port, but you’re giving it a UNC path. If I had a file on my local computer, and I wanted to put it on [PC1.domain.com](http://PC1.domain.com) in c:\temp Then I’d do something like this:

```powershell
Copy-Item -Path .\blah.txt -Destination 'c:\temp\' -ToSession $Port

```

Knowing the following is going to help you write your code:

1. Where the code is being ran from
2. Where the file currently exists
3. Where it needs to go.

---

<div class="post-metadata">

**Author:** ![wilson](https://avatars.discourse-cdn.com/v4/letter/w/96bed5/32.png) [@wilson](https://forums.powershell.org/u/wilson)\
**Post date:** [April 25, 2024, 4:33pm UTC](https://forums.powershell.org/t/copy-to-remote-pc/24004/4 "2024-04-25T16:33:23Z")

</div>

Hi Folks  
I didn´t understand why, but I just separated the instructions in two files with the tip neemobeer told me.  
First file.ps1: get-aduser…,  
Second ps1: from variable $port…  
Now worked 🙂  
Thank you so much

---

<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:** [April 25, 2024, 5:33pm UTC](https://forums.powershell.org/t/copy-to-remote-pc/24004/5 "2024-04-25T17:33:45Z")

</div>

Your Destination was incorrect is why 😉 I don’t think it has anything to do with multiple files, and neemo didn’t recommend separate files either.

Take a closer look at the docs:

[Copy-Item (Microsoft.PowerShell.Management) - PowerShell | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-7.4#-tosession)

From the ToSession parameter definition:

" Specify the **PSSession** object to which a remote file is being copied. When you use this parameter, the **Destination** parameter refers to the local path on the remote machine."

Key phrase there is _local path_ your original code references the _remote_ or UNC path

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 25, 2024, 5:34pm UTC](https://forums.powershell.org/t/copy-to-remote-pc/24004/6 "2024-05-25T17:34:32Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
