How to Copy File to Remote Computer with PowerShell
How to use PowerShell to copy files to remote computers is a concern for many users, as it is very convenient and useful to use Copy-Item command for file copying, especially when managing multiple computers in network environment.
---Question from a Reddit user
PowerShell's Copy-Item command not only avoids the tedious process of copying files manually, but also brings the following benefits:
🔹Automation: Use scripts to automate file copying and reduce human error.
🔹Batch operations: Easily copy files or folders to multiple computers.
🔹Security: PowerShell ensures that file transfers are protected.
🔹Error handling: Scripts can be viewed during file transfers to respond to and resolve error issues.
Next, we'll take a quick look at the simple steps involved in copying file to a remote computer using powershell, and then we'll go into more detail in the next section:
Key Steps | Command |
---|---|
1: Create a New Remote Session | Cmdlet: New-PSSession |
2: Verify the Session Properties | Cmdlet: Get-PSSession |
3: Copy File / Item from One Location to Another | Cmdlet: Copy-Item |
4: Verify Whether the File Has Been Copied | Cmdlet: Invoke-Command |
5: Terminate the Remote Session | Cmdlet: Remove-PSSession |
How to Copy File to Remote Computer Using PowerShell
Copying a file to a remote computer with PowerShell commands can be confusing for users, so we will show the process in the simplest way possible.
Step 1: Run PowerShell
Press Win + X on your keyboard and run PowerShell as administrator.
Step 2: Create a Connection to Remote Computer
You need to create a new remote session with the remote computer using the computer name and credential and store the result in a variable. Please use the following command:
$Session = New-PSSession -ComputerName "XXX" -Credential "XXX"
Step 3: Verify the Session Properties
Enter the following command to verify the properties of the remote session:
Get-PSSession
Then, you will see the returned command output.
Step 4: Copy a File
To copy a file to a remote computer using the Copy-Item command, you need to specify the paths to the local and destination files and specify the ToSession parameter. Please use the following command:
Copy-Item "C:\Folder\test.txt" -Destination "D:\Folder_Copy\" -ToSession $Session
Step 5: Verify Whether the File Has Been Copied
Use the following command to verify that the file has been copied:
Invoke-Command -ScriptBlock { Get-ChildItem -Path "C:\Folder\test.txt" } -Session $SESSION
Then, you will see the returned command output.
Step 6: Terminate the Remote Session
After you have finished copying the file remotely, enter the following command to terminate the remote session:
Remove-PSSession -Session $SESSION
Other Commands for Copying Items in Remote Session
Copy-Item is a PowerShell command used to copy items from one location to another. It can not only copy a single file to the remote machine, but also perform other copy operations in the remote session. Please read the following section:
Operation | Copy-Item Cmdlet | Note |
---|---|---|
Copy a folder to a remote computer | Copy-Item "D:\Folder\" -Destination "C:\Folder_Copy\" -ToSession $Session | No subfolders or files are copied if the Recurse parameter is not used. |
Recursively copy the entire contents of a folder to a remote computer | Copy-Item "D:\Folder\" -Destination "C:\Folder_Copy\" -ToSession $Session -Recurse | Using the Rescue parameter, the subfolder is copied with its file tree intact. |
Copy a file to a remote computer and rename the file | Copy-Item "D:\Folder\test.txt" -Destination "C:\Folder_Copy\test_copy.txt" -ToSession $Session | To rename the file, do not add the / symbol at the end of the file or folder name. |
Copy a file from a remote computer to local | Copy-Item "C:\Folder\test.txt" -Destination "D:\Folder_Copy\" -FromSession $Session | The FromSession parameter is used for the copy command from the remote computer. |
Conclusion
After introducing the operation of using PowerShell to copy files to a remote computer, you will become familiar with Copy-Item command and learn to use it. Alternatively, you can use the file transfer feature of third-party remote access software, which is also a great way to copy files to different locations.
Leave a Reply.