# How to use SSH in Powershell

With PowerShell SSH, you can access and manage remote systems from virtually anywhere. In addition, SSH offers detailed **logging** functions that allow activities on the server to be comprehensively monitored and tracked.

## What are the requirements for PowerShell SSH?

Integrating SSH in Windows PowerShell gives you the ability to establish SSH connections to remote systems and execute **SSH commands** in a [PowerShell script](https://www.ionos.com/en-ie/digitalguide/server/configuration/create-a-powershell-script/). This makes it a lot easier to manage and automate tasks in distributed IT infrastructures, especially in a heterogeneous environment where there are both Windows and non-Windows systems.

Here are the requirements to use SSH in PowerShell:

- **PowerShell installation**: Make sure that PowerShell v6 or higher is installed on your local computer. PowerShell is usually pre-installed in Windows, but you need to check that you are using a version that supports SSH.
- **SSH client**: You need an SSH client in your PowerShell environment to establish SSH connections. OpenSSH is available by default in Windows 10/11 and Windows Server 2019. If you are using an older version of Windows, you may need to install OpenSSH manually.
- **SSH server**: The SSH server is the target server that you want to connect to. This can be a Linux server, a network device or another SSH-capable host.
- **Network access**: Your local computer should have access to the network and be able to reach the remote SSH server. Port 22 must be open for SSH in the firewall rules.
- **PowerShell modules**: There are PowerShell modules that have been specially developed for the use of SSH, such as the ‘PSSession’ or ‘Posh-SSH’ module. You should check that the corresponding module is installed on your system in order to be able to use SSH functionality in PowerShell.
- **Authorisations**: You need administrator or elevated privileges and a password or SSH key to establish SSH connections from your local computer and access the remote server.

## Step-by-step instructions on how to use PowerShell SSH

Before you can use Secure Shell (SSH) in PowerShell, you must set up the **OpenSSH server**. You can then start the PowerShell SSH session and execute commands on the remote system.

### Step 1: Install OpenSSH

Click on the start menu or enter ‘PowerShell’ in the search bar. Select **Run as administrator**. This opens the Windows PowerShell. You can use the following PowerShell command to install the OpenSSH feature on your Windows computer:

```powershell
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
```

### Step 2: Start OpenSSH service

Once the installation is complete, you can start the OpenSSH service:

```powershell
Start-Service sshd
```

You can check whether the OpenSSH service is running:

```powershell
Get-Service sshd
```

The **Running** status indicates that sshd is running properly.

[![Image: PowerShell: SSH server service](https://www.ionos.com/en-ie/digitalguide/fileadmin/_processed_/8/c/csm_powershell-get-service-ssh_a9063b610b.webp "PowerShell: SSH server service")](https://www.ionos.com/en-ie/digitalguide/fileadmin/DigitalGuide/Screenshots_2023/powershell-get-service-ssh.png) Get-Service sshd checks the execution of the SSH service. ### Step 3: Start SSH server service automatically

To activate the OpenSSH server service **every time Windows is restarted**, you can enter the following command:

```powershell
Set-Service -Name sshd -StartupType 'Automatic'
```

### Step 4: Customise firewall

If Windows firewall is active, you should add a rule to allow SSH traffic on **Port 22 (the default SSH port)**.

```powershell
New-NetFirewallRule -Name 'OpenSSH-Server' -DisplayName 'OpenSSH Server' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
```

### Step 5: Establish an SSH connection

You can now log in with your SSH server from another device. To do this, open PowerShell on your client PC and enter the SSH command with the **username and server address**.

```powershell
ssh username@servername
```

Replace ‘username’ with your SSH username and ‘servername’ with the IP address or hostname of the remote server. When you start the command, you will be prompted to enter your SSH password unless you are using the **SSH key** for authentication.

[![Image: PowerShell: SSH login](https://www.ionos.com/en-ie/digitalguide/fileadmin/_processed_/a/5/csm_powershell-ssh-login_05553cb132.webp "PowerShell: SSH login")](https://www.ionos.com/en-ie/digitalguide/fileadmin/DigitalGuide/Screenshots_2023/powershell-ssh-login.png) Enter the SSH password ### Step 6: Work in the SSH session

Once you have established an SSH connection, you can execute commands on the remote server as if you were physically logged on to the server. To end the SSH session and return to the local PowerShell, you can use the `exit` command.

There are a number of SSH commands in PowerShell. The command `pwd` stands for ‘print working directory’ and shows you the current working directory, for example:

[![Image: PowerShell SSH commands using PWD as an example](https://www.ionos.com/en-ie/digitalguide/fileadmin/_processed_/f/e/csm_powershell-ssh-commands-example_4f8b6f0a09.webp "PowerShell SSH commands using PWD as an example")](https://www.ionos.com/en-ie/digitalguide/fileadmin/DigitalGuide/Screenshots_2023/powershell-ssh-commands-example.png) Example for SSH commands in PowerShell Here is a list of commands that you can use in PowerShell SSH:

- `ls/dir`: Displays the contents of a directory
- `cd`: Changes the directory on the remote server
- `touch/New-Item`: Creates a new file
- `rm/Remove-Item`: Deletes files or directories
- `mv/Move-Item`: Moves or renames files and directories
- `useradd`: Adds a new user
- `scp`: Transfers files between client and server

You can find more commands in our overview of [PowerShell commands](https://www.ionos.com/en-ie/digitalguide/server/configuration/powershell-commands/).


This is a markdown version of: [https://www.ionos.com/en-ie/digitalguide/server/configuration/powershell-ssh/](https://www.ionos.com/en-ie/digitalguide/server/configuration/powershell-ssh/) for AI/LLM consumption.