A.K.A How to Run PowerShell on the Raspberry PI
Ok you want to run PowerShell scripts on your Raspberry PI. Well look no further because I have a detailed step by step guide below!
This guide assumes you have already connected your Raspberry PI to your network either by a cable or via wireless.
This guide also assumes you prefer to develop the code on your Windows PC using a program with similar features to the PowerShell ISE.
- Setup Your Raspberry Pi For Remote Access
- Install Microsoft Visual Studio Code
- Install The PowerShell Extension
- Install The Remote Development Extension
- Connect To Your Raspberry Pi
- Install PowerShell To Your Raspberry Pi
- Install The PowerShell IOT Module
- Run Some Code!
Step 1: Setup Your Raspberry PI For Remote Access
First thing we need to do is enable SSH for remote access to the Raspberry Pi and also enable any additional protocols you want to use in your electronics projects.
Click the Menu -> Preferences -> Raspberry Pi Configuration
Enable SSH and if you wish to use SPI or I2C in your projects then go ahead and enable these and then press ok.
That’s it on the Raspberry Pi for now. You can now go to your Windows PC and continue with the setup.
Step 2: Install Microsoft Visual Studio Code
Visual Studio Code is a code editor, think of it as a cut down version of Visual Studio similar to the PowerShell ISE. It will let you remotely connect to a Raspberry Pi, edit code stored on the Pi and allow you to run scripts on it, all from our Windows PC.
Download it from here: https://code.visualstudio.com/
Run through the setup and accept all of the defaults. Tick the box to add a shortcut to your desktop if you want.
Step 3: Install The PowerShell Extension
Fire up Visual code then click on the Extension menu from the far left panel. Type ‘PowerShel’ into the search box at the top and click the little green ‘Install’ button.
This will allow Visual Code to properly format the code and enables IntelliSense so you can write code more efficiently.
Step 4: Install The Remote Development Extension
Now Search for ‘Remote Development’ and click the little green ‘Install’ button. This allows Visual Code to remotely connect to your Raspberry PI so you can create scripts and remotely execute them.
Step 5: Connect To Your Raspberry Pi
Click on the Remote Connection icon from the far left panel then click on the gear icon to configure a target.
Click on the first item that appears. It should automatically point to c:\users\<your username>\.ssh\config
This will open up an SSH config file. This is where you enter the hostname and username of your Raspberry Pi. If you have kept the default Raspberry Pi host name and user name then copy the options shown in the screen shot below.
Once updated, Click on File then Save.
The Raspberry Pi should now be listed under the Targets menu. Right click the Raspberry Pi and click ‘Connect to Host in Current Window’.
You will be prompted to enter the password for your Raspberry Pi. If you have not changed it them the default is ‘raspberry’.
Next click on ‘Open Folder’ and you should see a list of all files and folders on the Raspberry Pi.
Step 6: Install PowerShell To Your Raspberry Pi
If you dont see the Terminal window at the bottom click on ‘View’ then ‘Terminal’. This is a remote SSH session into the Raspberry Pi. This is where we will launch PowerShell and scripts from once it’s been installed.
To install Powershell I have created a handy script you can run.
In this example I have created a scripts folder then in there created a new file called ‘InstallPowerShell.sh’.
In the InstallPowerShell.sh file copy and paste this script to it and save the file.
sudo apt Get install libunwind8 wget https://github.com/PowerShell/PowerShell/releases/download/v6.2.2/powershell-6.2.2-linux-arm32.tar.gz mkdir ~/powershell tar -xvf ./powershell-6.2.2-linux-arm32.tar.gz -C ~/powershell sudo ln -s ~/powershell/pwsh /usr/bin/pwsh sudo ln -s ~/powershell/pwsh /usr/local/bin/powershell sudo WIRINGPI_CODES=1 pwsh
Now in the Terminal window type:
cd scripts chmod +x InstallPowerShell.sh ./InstallPowerShell.sh
Now sit back for a minute or so and let the magic happen!
If all works successfully PowerShell should fireup in the Terminal window.
Step 7: Install The PowerShell IOT Module
Since you are installing PowerShell onto your Raspberry Pi there is a good chance it’s because you want to connect some electronic devices, sensors, LED’s or Segment displays to it. Cool! you will now need to install the PowerShell IoT Module.
Into the PowerShell session type the following commands:
Install-Module Microsoft.PowerShell.IoT Import-Module Microsoft.PowerShell.IoT Get-Command -Module Microsoft.PowerShell.IoT
If everything goes as planned you should see a list of the IoT module cmdlets available for controlling the pins and interfacing with SPI and I2C devices!
Now each time you open Visual Code it will prompt you to enter the Raspberry Pi’s password and connect to it. When you want to fire up a PowerShell session in the Terminal window simply type:
PowerShell will fireup and from there you can run your scripts just as if you were running them locally on your PC but they are actually running on the Pi.
To run a script .\scriptname.ps1 from the terminal window
Running PowerShell on the Raspberry Pi is great fun and opens up a world of possibilities. Combine your PowerShell scripting skills with some electronic sensors and displays and you have got yourself a good time 🙂