<img alt="" src="https://secure.hims1nice.com/151009.png" style="display:none;">
" style="background-color: #2c3e50;">


Renaming a Windows 10 IoT Core Device via PowerShell

Posted by Mike Branstein on Aug 31, 2016 11:34:55 AM
Mike Branstein
Find me on:

This post is the second of a series of posts describing our automation efforts for provisioning Windows 10 IoT core on a Raspberry Pi. (Click here to read the first post)

To recap, KiZAN has a lab of 26 Raspberry Pi 2 boards running Windows 10 IoT core. On a regular basis, we need to re-flash, provision and configure the boards. When we perform the re-flash and provisioning process, it is manual and consumes more time than we want to spend (up to 2 days). In our first post, you learned how to find the IP address of a Windows 10 IoT core device after the initial flash. The second step in our automation journey is automatically renaming a device. In this post, you'll learn how to use PowerShell to rename a Windows 10 IoT Core Device after its initial flash.

Windows 10 IoT Core Device


Posts in This Series:

26 Devices all Named minwinpc
In our previous post, I discussed the problem of having 26 devices all named minwinpc. We resolved this issue by referencing machines by IP address; however, this isn't a long-term solution. Ideally, we'd like to use DNS to reference each machine.

Now that I have the IP address of each unit, it's pretty easy to rename them. 

At a high-level, you:

  • Identify a device via IP address (done)
  • Add the device to the Trusted Hosts list
  • Establish a remote PowerShell session with the device
  • Remotely run a script to rename the device 

Adding a Device to the Trusted Hosts List 
Before you can establish a remote PowerShell connection to a device, you need to add the device to your machine's Trusted Hosts. The Trusted Hosts file is a list of devices that your computer references to know whether it's safe to establish a connection.

To add a device to the Trusted Hosts list, run this PowerShell command: 

This works great…until you provision multiple devices simultaneously on separate threads. Every time this code is run, it overwrites the Trusted Hosts to only include the IP address passed in. This happens because Trusted Hosts is a computer-wide value. With some tweaking, I was able to get each device to add to the Trusted Hosts without overwriting the value already there.

Line 1 gets the path to Trusted Hosts. Line 2 checks to see if the value of $trustedHosts is empty and if so I add the device (this will only happen on the first device I provision). After the first device has been added, I check to see if $trustedHosts does NOT contain the IP address connected to the device I am adding. If it does include it, then it exits the if statement because it already exists. If it isn't there and the value isn't empty, add the IP address to the list already present. The -force is used because when you set a new Trusted Host, it asks you if you trust to add this device. I don't want to click yes every time I run the scripts, so this overrides the yes.

Establish a Remote PowerShell Session 
A remote PowerShell session establishes a connection to a computer (Windows 10 IoT core devices in our case) and allows you to run Windows PowerShell commands and scripts on the device. You can use the following code to establish a remote PowerShell session.

The first several lines establish secure credentials for the remote device. Next, I create a time-out of 5 minutes (this is in milliseconds), because it takes some time to connect to the remote session. The last line starts a new session on the $ipAddress that is passed in with the credentials to that device. This stores the current session in the $session variable to be referenced later to run scripts on the session.

Remotely Run the Rename Script
For the renaming, I felt it made sense to put the code in a separate script, then pass the script into the remote PowerShell session (separation of concerns, am I right?). I also did this to make sure the commands are run on the device, not on your own local machine. I learned this the hard way by putting a reboot in the provisioning script without using a session and restarted my own computer…but, that’s beside the point. I used the following script to change the device's name.

This script takes a parameter with the device's name and renames the device with the SetComputerName command. After the rename command has been run, it outputs "Computer name changed successfully. Please reboot the device for changes to take effect." The restart of the device will come in a later post.

To run the script remotely, include it in the following script:

The Full Code
This post showed a tutorial on Renaming a Windows 10 IoT core device via PowerShell. In the next post, you'll learn how to install WiFi drivers in Windows 10 IoT core.

Many thanks to Ryan DiChiara, one of our summer interns for doing a lot of the legwork (and writing) this post!


Check out our free, hands-on, IoT development workshop series!

Upcoming Events

Posted by Mike Branstein

Developer and leader. Passionate about systems architecture, domain driven design, application lifecycle management, and technology.

Topics: IoT (Internet of Things)