PowerShell File Transfer
PowerShell Remoting (WinRM) can be used for file transfer operations when HTTP, HTTPS, or SMB are unavailable.
PowerShell Remoting Overview
PowerShell Remoting allows us to execute scripts or commands on a remote computer using PowerShell sessions. Administrators commonly use PowerShell Remoting to manage remote computers in a network, and we can also use it for file transfer operations.
By default, enabling PowerShell remoting creates both an HTTP and an HTTPS listener:
- TCP/5985 for HTTP
- TCP/5986 for HTTPS
Requirements
To create a PowerShell Remoting session on a remote computer, you will need:
- Administrative access, OR
- Be a member of the Remote Management Users group, OR
- Have explicit permissions for PowerShell Remoting in the session configuration
Testing Connectivity
Test if WinRM port is open:
Test-NetConnection -ComputerName DATABASE01 -Port 5985
Creating a PowerShell Session
Create a PowerShell Remoting session to a remote computer:
$Session = New-PSSession -ComputerName DATABASE01
If credentials are needed:
$Credential = Get-Credential
$Session = New-PSSession -ComputerName DATABASE01 -Credential $Credential
File Transfer Operations
Copy File to Remote Session
Copy a file from localhost to the remote session:
Copy-Item -Path C:\samplefile.txt -ToSession $Session -Destination C:\Users\Administrator\Desktop\
Copy File from Remote Session
Copy a file from the remote session to localhost:
Copy-Item -Path "C:\Users\Administrator\Desktop\DATABASE.txt" -Destination C:\ -FromSession $Session
Common Cmdlets
New-PSSession: Create a new PowerShell remoting sessionCopy-Item: Copy files to/from remote sessions-ToSession: Copy to remote session-FromSession: Copy from remote session
Test-NetConnection: Test network connectivity to a remote host