Install PowerShell on Home Assistant

You can use powershell scripts in Home Assistant. i will show you in this post how it works. But, I’m not using it anymore for two reasons:

  • Performance: Powershell uses a lot of Resources on this small system and slowed it down. for most actions, there are beter alternates already implemented in Home Assistant
  • Updates: after every Home Assistant update, i had to reinstall PowerShell, cause it was removed during the update.

But if you are still interested in how to do it, you can do it like that:

Assistant works with docker and depending on the docker instance you have to connect it in another way

First of all you need the AddOn “SSH & Web Terminal” (not Terminal & SSH). After installing the AddOn, you have to configure it:

  • set the Protection mode to off
  • if wished, you can set Show in sidebar to on
  • set the network port
  • restart the AddOn and verify, that it is loaded trought the AddOn-Logs

then connect to the Terminal

alternatively you can also connect to the Terminal by SSH

connect to the docker instance:

sudo su
docker exec -it homeassistant /bin/bash

To install PowerShell inside a Home Assistant instance you have first to find out, what kind of Operating System is behind Home Assistant.

cat /etc/os-release

now it’s clear that there is an Alpine Linux installed, with this information, you will found official documentation for installing PowerShell under Alpine Linux:

Installing PowerShell on Alpine Linux – PowerShell | Microsoft Learn

now create a new folder below the config folder

mkdir /config/pwsh

now you can browse with Studio Code Server to the new folder and create a PowerShell file. To edit powershell file, it is recommended that you install the AddOn “Studio Code Server” (VSCode) and then install the PowerShell Extension inside Studio Code Server:

when you open now a PowerShell file inside VSCode, you will receive an error, that PowerShell is still not installed. That’s cause inside the VSCode Terminal it’s another container with another linux distribution.

install powershell inside the zsh terminal like described here:

Installing PowerShell on Debian Linux – PowerShell | Microsoft Learn

After installing it, you can call your powershell scripts from your sensors like this:

sensor:
  - platform: command_line
    name: gas_sensor_ps
    json_attributes:
      - gas_sensor_value
      - muffling
      - gas_sensor_state
    command: "pwsh /config/pwsh/scripts/gassensor.ps1"
    value_template: "{{ value_json.gas_sensor_value }}"
    scan_interval: 15