Copy files to an Azure VM through PSRemoting

You may sometimes need to copy a file to a remote machine where the SMB (aka cifs) ports are closed, or the option to copy files through the RDP connection is disabled as well. Also, you may need to copy the file(s) as a step in an automated process.

If you have PowerShell 5.0 installed it’s easy as pie, since the Copy-Item cmdlet has a set of new parameters: -ToSession and -FromSession

First, create a PSSession object

$cred = Get-Credential
$sess = New-PSSession -ComputerName -Port 5986 -Credential $cred

Then, use the Copy-Item cmdlet with the –ToSession parameter and pass the PSSession object you created in the previous step:

Copy-Item -Path -Destination C:Temp -ToSession $sess

You can use the same technique to copy files from the remote VM:

Copy-Item -FromSession $sess -Path C:\Temp\ -Destination C:\Temp

Note that the ToSession and FromSession parameters are mutually exclusive parameters (are not included in the same parameter set), meaning you cannot use them to copy files directly from one session to another.