Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

Copy items using psremote-session

tags: PowerShell categories: PowerShell-Remoting

Table of Contents

Copy items from a local to a remote computer using a psremote-session.

Copy-ToVirtualMachine

function Copy-ToVirtualMachine{

    <#

    .SYNOPSIS
       Copy to remote computer

    .DESCRIPTION
       Copy content from the local computer to a remote computer using psremote-session

    .PARAMETER ComputerName
       Remote computer to copy to

    .PARAMETER creds
       Credentials of the remote computer

    .PARAMETER Source
       Source folder on the local computer
 
    .PARAMETER Destination
       Destination folder on the remote computer
 
    .EXAMPLE
    Copy-ToVirtualMachine -ComputerName 'Server01' - creds $creds -Source 'C:\Export\*' -Destination 'C:\Import'

    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [String] $ComputerName,

        [Parameter(Mandatory=$true)]
        [PSCredential] $creds,

        [Parameter(Mandatory=$false)]
        [String] $Source = 'C:\Export\*',

        [Parameter(Mandatory=$false)]
        [String] $Destination = 'C:\Import'
    )

    $function = $($MyInvocation.MyCommand.Name)
    Write-Host "$(Get-Date -f 'yyyy-MM-dd HH:mm:ss.ffff') Starting $function" -ForegroundColor Yellow
    $rsession = New-PSSession -ComputerName $ComputerName -Credential $creds
    Copy-Item -ToSession $rsession -Path $Source -Destination $Destination -Force -Recurse
    Remove-PSSession -Session $rsession
    Write-Host "$(Get-Date -f 'yyyy-MM-dd HH:mm:ss.ffff') End $function" -ForegroundColor Yellow
}

Copy-FromVirtualMachine

function Copy-FromVirtualMachine{

    <#

    .SYNOPSIS
       Copy from a remote computer

    .DESCRIPTION
       Copy content from a remote computer to the local computer using psremote-session

    .PARAMETER ComputerName
       Remote computer to copy from

    .PARAMETER creds
       Credentials of the remote computer

    .PARAMETER Source
       Source folder on the remote computer
 
    .PARAMETER Destination
       Destination folder on the local computer
 
    .EXAMPLE
    Copy-FromVirtualMachine -ComputerName 'Server01' - creds $creds -Source 'C:\Export\*' -Destination 'C:\Import'

    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [String] $ComputerName,

        [Parameter(Mandatory=$true)]
        [PSCredential] $creds,

        [Parameter(Mandatory=$false)]
        [String] $Source = 'C:\Export\*',

        [Parameter(Mandatory=$false)]
        [String] $Destination = 'C:\Import'
    )

    $function = $($MyInvocation.MyCommand.Name)
    Write-Host "$(Get-Date -f 'yyyy-MM-dd HH:mm:ss.ffff') Starting $function" -ForegroundColor Yellow
    $rsession = New-PSSession -ComputerName $ComputerName -Credential $creds
    Copy-Item -FromSession $rsession -Path $Source -Destination $Destination -Force -Recurse
    Remove-PSSession -Session $rsession
    Write-Host "$(Get-Date -f 'yyyy-MM-dd HH:mm:ss.ffff') End $function" -ForegroundColor Yellow
}

Call the functions

$remotehost  = 'W2K16RC-SRV03'
$remotecreds = Get-Credential -Message "Enter the credentials to connect to $remotehost" -UserName 'Administrator'

Copy-ToVirtualMachine -ComputerName $remotehost - creds $remotecreds -Source 'C:\Export\*' -Destination 'C:\Import'

Copy-FromVirtualMachine -ComputerName $remotehost - creds $remotecreds -Source 'C:\Export\*' -Destination 'C:\Import'

See also

New-PSSession and Copy-Item on Microsoft Docs.

[ Top ] [ Blog ]