Home How-To-Guides Streamlining File Transfer from Windows to Linux with PowerShell: A Step-by-Step Guide

Streamlining File Transfer from Windows to Linux with PowerShell: A Step-by-Step Guide

Posted: December 11, 2023

a computer screen with a program running on it

Introduction

PowerShell, created by Microsoft, is a powerful cross-platform task automation solution built on the .NET framework. It includes a command-line shell and scripting language, which can be used for various system administration tasks. While it was initially designed for Windows, the newer versions support different platforms, including Linux and macOS. An important aspect of its functionality is the ability to copy files from one system to another, which can be especially useful in a diverse infrastructure comprising Windows and Linux systems.

Importance of PowerShell

PowerShell presents a robust and comprehensive framework for managing and automating tasks on Windows. It streamlines and simplifies various administration tasks, from managing files to controlling and monitoring system services. It allows admins to work with the .NET objects directly, enhancing its functionality and flexibility.

Moreover, with the SSH (Secure Shell) capability introduced in PowerShell 6.0, communication between two hosts to exchange data has been made easier. This beneficial feature enables PowerShell remoting, which allows admin tasks to be carried out over a network involving multiple systems, regardless of the operating system they are running on. This SSH-based remoting is particularly beneficial for remoting between Windows and Linux computers. It requires SSH to be correctly installed and configured on both the local system (from where files are to be copied) and the destination system (to where the files are to be copied).

Brief on Copying Files from Windows to Linux

Copying files between Windows and Linux machines can often present a challenge due to their different file systems and protocols. However, PowerShell makes this task straightforward. The Copy-Item cmdlet in PowerShell is used to copy an item from a source location to a destination.

One prerequisite to achieving this is the creation of a PowerShell remoting session. A remoting session can be created between the Windows and Linux machines using the SSH protocol. Microsoft primarily recommends the New-PSsession command for creating a new PowerShell session. The syntax requires the user to specify the host IP and username of the remote Linux machine.

After establishing the remoting session, the user needs to select the file(s) to copy from the Windows machine. The files can simply be right-clicked, and the 'Copy as path' option can be selected to copy the file path. The dir command can also be used to display the contents of a directory and select a file for copying. The copied file path can then be used with the Copy-Item command to initiate the file copy operation to the Linux machine.

Upon successful execution of the command, the selected file(s) will be copied from the Windows machine to the specified destination on the Linux machine. Thus, with simple steps and commands, PowerShell provides a simple way to copy files from a Windows computer to a Linux machine.

Pre-requirements

Before copying files from Windows to Linux using PowerShell can be completed, a few prerequisites must be met. Understanding these steps helps ensure a smoother process.

Update the PowerShell version to 6 or above

PowerShell has evolved over the years, with each version bringing improvements and new features. Beginning with version 6.0, PowerShell started supporting the SSH protocol, which opened avenues for PowerShell remoting to non-Windows platforms, such as Linux. Therefore, to use the SSH remoting feature to copy files from Windows to Linux, PowerShell on your Windows computer must be version 6 or higher. You can check the current PowerShell version by running $PSVersionTable.PSVersion in the PowerShell window.

Install PowerShell on the target Linux computer

Unlike the earlier versions, the newer versions of PowerShell are cross-platform and support Linux. Therefore, for PowerShell remoting to work, you must install PowerShell on the target Linux computer as well. PowerShell can be easily installed from GitHub. This installation will include all the features necessary for the SSH remoting.

Establish a Remote Session Using SSH Remoting

With PowerShell 6 and above, SSH can enable true multiplatform PowerShell remoting between Windows and Linux systems. The SSH remoting allows the creation of a PowerShell host process on the target Linux computer as an SSH subsystem.

To establish a remote session using SSH, use the New-PSSession, Enter-PSSession, and Invoke-Command cmdlets. These cmdlets have a new parameter set to support SSH remoting. To create a remote session, use the HostName parameter to specify the target Linux computer and the UserName parameter to provide the username. When running the cmdlets interactively, you will be prompted for a password. Alternatively, you can use SSH key authentication by specifying a private key file with the KeyFilePath parameter.

The SSH server on the remote Linux system must be configured to create a PowerShell process for the SSH subsystem. You should enable password or key-based authentication on the Linux system as well. More detailed instructions for creating keys for SSH authentication can be found in the appropriate platform documentation.

Steps to Copy Files from Windows to Linux Using PowerShell

This section will go trhough the steps involved in copying files from a Windows machine to a Linux machine using PowerShell. This process involves creating a remote PowerShell session, selecting the file path to be transferred, and using the Copy-Item cmdlet to initiate the file transfer.

Create a New PowerShell Remote Session

The first step is establishing a PowerShell remote session between the Windows and Linux machines. To create a remote PowerShell session, Microsoft recommends the New-PSSession command. This command should include the hostname (IP address) and the remote Linux machine username. Ensure that the host IP and username are replaced with the relevant information for your specific Linux machine. The username provided should correspond to the one on the Linux host computer.

Select and Copy the File Path You Want to Transfer

Next, select the file you wish to copy from the Windows machine. There are a few methods to achieve this, but perhaps the simplest is to right-click on the target file and select the 'Copy as path' option. This will copy the file's path, which can be later used with the Copy-Item command. Alternatively, you can also use PowerShell commands to retrieve the file path. First, navigate to the desired file's directory using the cd command. Once in the correct directory, use the dir command to display the directory's contents and locate the target file.

Use the Copy-Item Cmdlet to Start Copying

Once the remote session is active and you have the path to the file you want to copy, you can use the Copy-Item cmdlet to initiate the copying process. This cmdlet permits copying an item from a source location to a destination. The syntax would require you to replace 'file-path' with the original file path of the file you wish to copy and 'username' with the original username used earlier.

Upon entering the command, the file transfer will begin immediately. The file will be copied from the Windows machine and placed in the specified destination on the Linux machine. The procedure provides a convenient and robust solution for copying files from a Windows computer to a Linux machine using PowerShell.

Conclusion

In conclusion, there are manifold ways to transfer files from a Windows computer to a Linux machine. Tools and commands such as SCP (Secure Copy) and PSCP (PuTTY Secure Copy Protocol) are commonly used for this. They both leverage the SSH protocol for secure file transfer. Yet, as we've seen throughout this guide, one can harness the power of PowerShell to accomplish this task in a streamlined and efficient manner.

Loading...