Powershell – Transfer files via Powershell Remoting (like scp in linux)

powershellscripting

During the creation of a few powershell-based deployment scripts, I sorely missed an easy way to quickly transfer a file to another server (over the internet) via Powershell Remoting, something like scp for linux.
Fortunately something that can be activated via Powershell Remoting. Did I overlook something?

Best Answer

You can easily copy the contents of a file over-the-wire through a PSRemoting session using Invoke-Command and Set-Content:

$Session = New-PSSession -ComputerName "remotehost.domain.tld" -Credential (Get-Credential) -UseSsl

$FileContents = Get-Content -Path 'C:\path\to\arbitrary.file'
Invoke-Command -Session $Session -ScriptBlock {
    param($FilePath,$data)
    Set-Content -Path $FilePath -Value $data
} -ArgumentList "C:\remote\file\path.file",$FileContents
Related Topic