In this, I’ll show you how to remotely shutdown (or really do anything with) a Windows 10 PC with Microsoft’s implementation of an OpenSSH server. This helps with controlling HTPCs or living room gaming PCs without installing anything third-party.
I’ll be making the assumption that you have physical or RDP access to the Windows computer that you want to control, and that you’re the Administrator of the computer. I’m also assuming you have a basic understanding of PowerShell. I’m also assuming that you’re running Hassio with an SSH addon or something equivalent, or otherwise have a Linux installation of Home Assistant and know how to operate SSH.
I basically combined a couple of different guides and suggestions to get to this, but this way you don’t need to figure as much out. Alright! Here we go.
First and foremost, you’re going to want to install OpenSSH Client and Server on your Windows PC. Open PowerShell with administrative access and run these two commands to install OpenSSH
# Install OpenSSH Client and Server Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0 Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Now, we’re going to start the SSH server service and make sure it always starts the service on boot
Start-Service sshd Set-Service -Name sshd -StartupType 'Automatic'
At this point, try connecting with another computer, or even your Hassio install. Your username will be whatever your local account name is. Not your Microsoft account. You can figure this out just by looking in
C:\Users\[one of these is your account]
Can you connect? Sweet! Now on to setting up key pairs so you don’t need those pesky passwords. Following GitHub’s guide to this is the easiest way to figuring this out, but I’ll copy the relevant steps here if you just wanna power through it.
After connecting to your Hassio machine, do the following:
# Generate the public and private key pairs on Hass.io # Just press enter through everything, you don't need to enter a passphrase ssh-keygen # Start ssh-agent in the background eval "$(ssh-agent -s)" # Add your keys ssh-add ~/.ssh/id_rsa # Next, copy your generated files to /config (or equivalent) so Home Assistant can access it cp ~/.ssh /config
Check that you now have a
.ssh folder in your
/config directory, containing at least
Now let’s add those keys to Windows! Copy the contents of
id_rsa.pub somewhere where you can access it on your Windows computer. The file should be one line, starting with
After logging into your account on Windows, open Explorer and go to:
If this folder doesn’t exist, create it. Create a new file called
authorized_keys with no file extension, open it in Notepad, and paste in the contents of the
id_rsa.pub file from earlier. Now we need to set permissions on the file.
- Right click
- Go to
- Click “Disable Inheritance”, then choose “Convert inherited permissions into explicit permissions on this object”
- Remove all permissions on the file except for
SYSTEMand your user. There should only be two entries in the list, both with “Full Control”
Now’s the most important step. After SSHing into your Hassio install, attempt to connect again to your Windows machine with
ssh [ local windows username]@[windows computer]
If it worked, and didn’t ask for a password, we’re in the home stretch. If it did, double check the instructions and check the sources for more details.
Setting up the wake_on_lan component
If you didn’t already have a
wake_on_lan switch, here’s the syntax
switch: - platform: wake_on_lan mac_address: "PC-MA-CA-DD-RE-SS" name: "Friendly name" host: "Computer's IP address to determine state" turn_off: service: shell_command.turn_off_yourpc # The shell_command is where the actual shutdown command takes place shell_command: turn_off_yourpc: 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [user]@[windows ip] shutdown /h /f' # You can also use a different shutdown command, but shutdown -h -f forces a hibernation
Passing a normal
'ssh [user]@[windows box] shutdown /h /f resulted in error 255, but this solution found by @keith-michael worked.
Let me know if you need anything else! This was probably a convoluted method but it worked for me.