Things used in this project

Story

Sometimes it is useful to be able to run various configuration scripts on your IoT device at start up. This article will outline a way to run a PowerShell script on startup of the Windows IoT Core operating system.

The first thing you will need to do is build the script you would like to run. In this example, we’ll keep it simple. We’ll create a log file at startup that will log the current date. Create a new file, name it StartupScript.ps1 and add the following lines of PowerShell code:

# startup script

$logFile = 'C:\startupLog.txt'

get-date >> $logFile

Next we will create a simple batch (*.bat) file that will execute the PowerShell script (or multiple scripts whatever your case may be). Create a new file, name it “Startup.bat” and populate it with the following code:

powershell -command "C:\StartupScript.ps1"

The next thing we will do is move the script and batch file to the IoT device. The device will need to be booted and on your network. On your PC, open file explorer, and in the address bar access your device by entering:

ftp://<your_iot_device_name_or_IP>

You will be prompted for credentials, use the Administrator credentials that you use on your IoT device. Once authenticated, navigate to your folder of choice (in my case I want to execute my script at the root as specified in my batch file) then drag and drop StartupScript.ps1 and Startup.bat to the FTP window in order to copy them over to your device.

FTP Files to Windows IoT Core

Next we will need to set the PowerShell execution policy on the Raspberry Pi to allow for remote signed scripts. On your PC, run PowerShell as an Administrator (Press the Windows button, and start typing PowerShell, right click on the icon and select “Run as Administrator”).

PowerShell as an Administrator

Follow the following tutorial to learn how to establish a PowerShell session with your IoT Core device. Now that you are connected to your device, we need to set the execution policy. From your PowerShell prompt, execute the following command:

Set-ExecutionPolicy RemoteSigned

You can now exit out of your PowerShell session. Next we will use Putty to access the command prompt of the IoT Core Device. Follow this tutorial to establish a Putty connection with your IoT Core Device. Now that you are at the command prompt, navigate to the “C:\Windows\System32” folder, alternatively you can also add to this folder permanently to your path by executing the following command (Optional):