Copy to Remote PC

Hi Folks

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

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

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…

-Destination "\\PC1\c$\fotos\Visitantes\" 
# should be
-Destination "C:\fotos\Visitantes\"
1 Like

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. Nontheless you need to clarify that.

Also take a closer look at the docs:

Copy-Item (Microsoft.PowerShell.Management) - PowerShell | Microsoft Learn

" 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 in c:\temp Then I’d do something like this:

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.

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 :slight_smile:
Thank you so much

Your Destination was incorrect is why :wink: 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

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

1 Like