Turn lights on and off when Windows 10 is locked or unlocked

Like many, I have smart lighting throughout my house. This includes my office and desk. Behind my beautiful ultrawide monitor, I have a 3m Philips Hue strip which helps add some peripheral light and softens the glare when using the screen. I use scenes to adjust the level of white light towards early evening, but I’ll save that for another day. While I can manage the lights in the room based on motion, I wasn’t doing anything with my monitor lights as I might have been working, but the room could have had no movement. This would have resulted in the monitor lights turning off and would have been frustrating, so I decided to integrate whether my workstation is locked or unlocked or not with Home Assistant.

My desk lights turning on when I unlock my Windows machine using Home Assistant

Previously I had a PowerShell script that I could run that would hit the Hue API to toggle my lights on or off but since I have moved away from my Hue bridge this no longer works plus it wasn’t automated, but this was a good starting point. The sysadmin in me remembered there was a trigger in Task Scheduler that would be a task when the workstation was locked or unlocked, which gave me the trigger for my Home Assistant integration.

Picture of the trigger screen in Task Scheduler showing the different triggers that are available.
Workstation lock/unlock triggers in Task Scheduler

The next part was talking to the Home Assistant to perform service calls such as light.turn_on or scene.turn_on. Fortunately, I know Home Assistant provides great extensibility and has a fantastic rest API, so that was Home Assistant. This just left the process to call the API. For this, I wanted to stick with PowerShell as it is very versatile and I use it all day long anyway.

I had a prototype working very quick, but I wanted something more modular and parameter-based to use for other things. This is when I took found Flemming Sørvollen Skaret’s Home Assistant PowerShell Module. His PowerShell module is awesome and really handy. I was off and running with it within minutes. So I decided to leverage this module in my Task Scheduler automation. And that was that, but I still had to hook it all together.

  • 2 x Task Scheduler Tasks – triggered when the workstation is locked and unlocked
  • 1 x VBScript to launch our PowerShell script so it is hidden
  • 1 x Flemming’s Home-Assistant PowerShell Module
  • 1 x PowerShell script to call the Home Assistant API
  • 1 x Home Assistant long-live token
  • 1 x entity in Home Assistant (a light, scene or input_boolean for example)

Task Scheduler

First I created two tasks in Task Scheduler. One for when the workstation locks and the other for when it is unlocked. When these tasks are triggered, they call a VBScript with some parameters. Yes. An actual 1900’s VBScript in 2020. For the life of me, I could not stop or prevent the PowerShell console from flashing up. I’m a UX nerd, and this wasn’t acceptable for me. I spent way too long on trying to solve this before I reverted to this well-known workaround of using WScript to execute the PowerShell script silently. These have been exported from Task Scheduler and therefore can be imported straight back in obviously correcting usernames and paths.

You will notice on line 46 above calls a common TaskSchedulerLauncher.vbs script along with the path to a ps1 file and the parameters to go with it wrapped in double-quotes.

Are you using a battery-powered device?

Geert van Horrik (@GeertvanHorrik) kindly pointed out that if you are using a battery-powered device like a laptop or a tablet you will need to change the conditions so the task runs when on battery power.

Power settings for a task in Task Scheduler


Having to use the VBScript is annoying but not as annoying as the PowerShell console flashing up each time I unlocked my workstation. It calls PowerShell.exe and our script while also setting the window style to be hidden.


In order to interact with the Home Assistant API you will need a long-lived token. You can create one in the Long-Lived Access Tokens on the profile page (http://localhost:8123/profile). Be sure to name it something sensible and meaningful.

The token and information about my Home Assistant instance are stored as variables in the script. The service I want to call such as light.turn_on, scene.turn_on and input_boolean.turn_on and the entity_id (input_boolean.james_desk) are passed to this script as parameters.

Additionally, as the PowerShell script contains the token to Home Assistant I store the script within my profile and limit the permissions to the file to just myself.

Home Assistant

I started off by just toggling the state of my monitor light but I shortly realised I could achieve more if I knew my workstation was unlocked so I created an input_boolean.

I toggle the state of this each time my Windows 10 machine is locked and unlocked. This means I can do more than just turn my lights on or off. I use this input_boolean in my motion automation which prevents my room lights from turning off if my workstation is unlocked (no more crazy waves). It also stops my Dyson fan from oscillating and directs it towards my desk and changes the speeds if the temperature is above a threshold. I can also choose route TTS notifications through to my desk Sonos speakers or HTML5 notifications to just my workstation.

Best of all with Windows Hello I just sit down at my desk, and Home Assistant handles everything else because it knows my workstation is unlocked and the chances are I’m working or tinkering with something at my desk.

I’ve also used the same PowerShell method to create a set of quick actions that I can call from buttons on a Home Assistant Stream Deck board where I can call my favourite playlists and scenes. I’ve also created some TTS scripts to announce and nag when chores need to be done or when it is dinner time! I think I’ll save that for another post!

I 💕 my Home Assistant

Replicate SharePoint Hub site navigation to other Hub sites

I have been working with a large government department where Microsoft Services helped them transform their intranet to SharePoint Online leveraging all the modern capabilities available as well as rethinking what an intranet was. This work included envisioning to fully exploit all of Office 365 and a crucial Information Architecture (IA) design which also mapped their existing, complex and poorly performing hierarchy of subsites to several new hub sites and a completely flattened site structure. I hope to write more about this work soon.

Primary intranet site navigation provider

To create a consistent user experience, the look and feel for several hub sites was replicated, creating the feel of a single intranet. The hub site navigation also needed to be replicated, and that is wherein the challenge lay. The navigation cannot be easily copied from one site to another, let alone between hub sites, and thus this solution was born.

We decided to create a dedicated hub site that only the intranet team can access. Within this hub site, we were then able to create the intended intranet navigation and get the headings, order and links just right without troubling anyone. Once we were happy with the navigation and testing successfully passed, the idea was that we would be able to replicate the hub site navigation to all our other hub sites. More importantly, the process could be easily repeated, keeping the intranet navigation updated across many hub sites. The task could also be automated but we opted to leave it as a manually initiated task.

Script to replication hub site navigation to other sites

The solution to the problem is a PowerShell script made up of two functions. One function that gets the navigation from the hub site acting as the master or primary navigation provider (supplied from a parameter). The other function replicates the navigation to other hub sites. The Get-SPOHubSiteNavigation function exports the hub site navigation from the site provided to a CSV file. The CSV file is then used by the Copy-SPOHubSiteNavigation function to replicate the navigation links to other sites. The process could be hijacked somewhat to add hub site navigation from a CSV file rather than reading it from an existing hub site. We found it was easier to get a feel for the navigation in a real SharePoint site rather than work with the information in a spreadsheet.

Screen recording of the Copy-SPOHubSiteNavigation replicating navigation between two hub sites.
Screen recording of the Copy-SPOHubSiteNavigation replicating navigation between two hub sites.

You can also disable the export process by setting the -Export parameter to -Export:$false. This switch makes the Get-SPOHubSiteNavigation quite useful for displaying the hub site navigation in an accessible collection. This collection can be used with pipe functions, to filter or sort the collection, for example. This collection is also much easier to work with than using the Get-PnpNavigationNode -Location TopNavigatioBar -Tree cmdlet and parameters to show the full navigation structure.

Hub site navigation collection displayed in a table.
Hub site navigation collection displayed in a table.


As with many of my posts, I like to share any observations I have made during the work.

  • The cached Hub navigation appears to refresh every 30 minutes if left unchanged. You can force the cache to refresh by editing the links and then clicking cancel.
  • A JSON file gets saved to the /_catalogs/hubsite library which could be related to the cached navigation. It has a GUID based name e.g. 20d31c78-8a8c-499a-b953-ecc673344cef.json and appears to get modified by the System Account when changes to the hub site navigation are saved.
  • The cached JSON file looks like this:
  • This JSON file, in theory, could be read from a source site and the navigation element copied and injected into the JSON on a target site through manipulation of the JSON payload and the /_API/navigation/SaveMenuState API.
  • When trying to add a new navigation node using the Add-PnpNavigationNode cmdlet. If the parent node was invalid I got the following error. This wasn’t because the URL linked to a file or folder that didn’t exist as the error below suggests. Dave Garrad kindly calls out the fix for thisThe fix is to add -External to the Add-PnPNavigationNode cmdlet to tell PowerShell that the link is not in the same site collection.
Add-PnPNavigationNode error
Add-PnPNavigationNode : error when no resource or file is available from the Url supplied.
  • The script could benefit additional error handling, particularly to ensure the target site(s) provided are registered as hub sites.
  • Bonus tip! If you find you have lost changes to the navigation, this same JSON file is part of a library where version history is enabled. This file appears in hub sites and sites associated with a hub site. The version history might help you recover from any loss, but you won’t be able to restore it as the library appears to be read-only.
Version history available for JSON file which I suspect ins the cached hub site navigation
Version history available for JSON file which I suspect ins the cached hub site navigation

I hope you find this post useful. As always #SharingIsCaring

PowerShell to manage the modern SharePoint Footer

So I’ll start by saying how bowled over I am by the engagement I’ve had after posting a tweet sharing that I had programmatically updated the new footer. For this, thank you, this kind of engagement and praise is why I love the community and contributing to it.

My tweet sharing my excitement after creating the PowerShell to manage the modern footer in SharePoint Online.

I want to share some numbers with you, which was one of the main reasons for pursuing this task.

  1. My customer had 180 sites where the footer needed configuring.
  2. Settings, change the look, select footer (three clicks)
  3. Enable footer, browser and select a logo and then upload it, enable displaying the footer name and then add the footer name text, apply settings and close settings (eight clicks)
  4. Edit links (one click), create a new link, add text, add a URL, save the link, then (four clicks) and then repeat for three other links (twelve further clicks). Then save the navigation (one clicks). Eighteen clicks for footer links.
  5. That is a total of 29 clicks per site — a total of 4,860 clicks for all these sites.

Almost five thousand clicks is a lot of mouse work and this number doesn’t even factor in browsing to each site nor typing each of the values and URLs, which leaves a lot of room for errors too!

Screen recording showing a click counter of the work involved in configuring the footer at scale.
Screen recording showing a click counter of the work involved in configuring the footer at scale.

#ThinkLean #WorkSmarterNotHarder

To emphasise just why I wanted to achieve this programmatically. Colleagues started to perform this manually while I did some R&D. During this time a collection of sites were updated. However, even with signed-off designs, change request and final confirmation, the customer still changed their mind hence why I like to code and work lean. This kind of change is not uncommon. However, for any organisation that centrally manages sites, has a flattened modern information architecture (IA) or a Hubified intranet with many sites where a standard and consistent footer might be wanted, not being able to manage the footer through PowerShell or other means is a problem. Even managing variations of a standard footer is likely to be an issue for if there is no way to handle it programmatically. 

The intranet team within a large government department I have been working closely with over the last two years are responsible for close to 200 sites. Performing tasks against these sites and making changes to them efficiently using PowerShell or other means is essential. Finally, we should not forget that trends, standards and regulations change over time. One such example is that regulations for internet and intranet sites within the government might require that certain information must be readily available from the footer, which would lead to a need for wide-spread changes. Even with their carefully designed flat site structure as part of their information architecture (IA) which leverages Hub sites this change is still site-by-site as the footer configuration is not inherited from a Hub site and therefore set independently.

Exploring how the Footer works

So with all this in mind, I started to explore how this could be achieved using PowerShell. The options and tools are limited and restricted given the environment. I felt that if this could be through the UI, then there must have been an API I could play with to achieve the same result. Off I went in my lab using Fiddler, SharePoint Online Client Browser, Edge Insider developer tools, Postman and VS Code. Troubleshooting and reverse engineering things is a childhood pastime. I found many other exciting easter eggs during this exercise that I hope to share too, but it was like there was a bounty on resolving the footer configuration.

Colleagues in my team were also looking for a solution to similar problems for their customers. A colleague quickly followed up about some undocumented verbs he found in the site design schema (see Site design JSON schema on Microsoft Docs). This news also confirms what I learned from some PG colleagues. Sadly, the footer only has site design support right now.

What I discovered through Edge tools that each change to the footer through the editing footer links or from the change the look pane invoked an API post to “/_API/SaveMenuState”. This post had a JSON payload with the configuration of the footer and also included any footer links. Win!

Developer Tools in Edge where I discovered the call to /_api/navigation/SaveMenuState
Developer Tools in Edge where I discovered the call to /_api/navigation/SaveMenuState
Full JSON payload sent to the SaveMenuState API in Edge Developer Tools.
Full JSON payload sent to the SaveMenuState API in Edge Developer Tools.

Initial approach and exploration

I took off with examples of this JSON payload that I obtained from Developer Tools after applying several different footer configurations. I started exploring how the API worked using Postman (this is an excellent tool by the way). To use Postman, I had to create an SP app to get an auth token from AAD so I could use Postman with SPO APIs. I gave the app full tenant access so I could explore things better. Getting started with Postman was a great lesson and one I want to share in a separate post, but Postman is now a vital tool for me when working with SharePoint Online. Once I had played with the API, I set out to achieve the same behaviour in PowerShell. 

Screenshot showing a API test in Postman
Testing different submissions to the API using Postman.

PowerShell Script

I set out with the intent to share my work as I know this was going to be of interest to lots of others. I decided to create multiple functions, so each part of the footer can be configured separately. I also built this with a view of contributing and sharing it as part of the SharePoint PnP module. The code is functional and not perfect. Let me walk you through it and share the entire thing at the end too.

Note: the script uses the debug log the output information ($DebugPreference = "Continue").

Enable/Disable SPO Footer – These two functions were created to quickly enabled and disable the footer with a simple function call.

Get/Set SPO Footer (Logo and Text) – It is useful to be able to get the configuration of both the footer text and the footer logo. These functions can be helpful to validate the configuration, for example. These both exist in the JSON as nodes with consistent GUID for their “title” and similar they both have a “key” that identifies them. To simplify the functions I created an overarching function to run them all.

Get/Add/Remove Navigation Links – This needs further work but it could leverage the existing Get/Add/Remove-PnpNavigationNode cmdlets. For my customer, I hardcoded the URLs in the JSON payload on line 417 but I provided a sample of how this looks on lines 408-415.

Sample Usage – Examples of how this can be used has been provided lines 435-471 but here is an example of the functions.

  • Enable/Disable-Footer – Enables and disables the footer
  • Get/Set-SPOFooter – Get the configuration of the footer
  • Get/Set-SPOFooterText – Set the footer text
  • Get/Set-SPOFooterLogo – Set the footer logo
  • Set-SPOFooterLinks – Create footer links

Full script

Here is the script with all the functions and some examples. I’m going to reach out to Vesa and the PnP team to see about getting this included somehow. But, for now, enjoy. If you have any issues or comments, please let me know!

Quirks to be aware of

During my testing, I noticed a number of quirks I had to work around or that I want to share and make you aware of.

  1. When using Postman between sessions, you must refresh your auth token to continue accessing SPO.
  2. No value is available when reviewing $site.FooterEnabled using the Get-PnPSite cmdlet so you cannot check to see if the footer is enabled or not.
  3. When getting the footer, I noticed there seem to be several different states for how the footer configuration which might impact the GET functions however I have tried to mitigate this risk.
    1. Disabled
    2. Enabled with no configuration
    3. Enabled, but the JSON appears similar to when it is it disabled
    4. Enabled with configuration and or links.
  4. I noticed some sites had the footer enabled after the footer feature was released, while others it was disabled, this was why I built the function to check how the configuration of the footer.

I hope you find this post helpful. Your engagement and feedback are what drive me to write posts like this, so please keep it up!


Switch between modern SharePoint homepages using PnP PowerShell

This featured in Episode 44 of the SharePoint Dev Weekly podcast.

I’ve been working on a modern intranet project amongst over projects for the last 12-months. This has been more about the transformation of content and business processes, rethinking information architecture and reimagining a modern intranet than it has been about custom development.

I’ve been working on a modern intranet project amongst over projects for the last 12-months. This has been more about the transformation of content and business processes, rethinking information architecture and reimagining a modern intranet than it has been about custom development.

We’ve recently been testing variations in the design of a homepage with different audiences. This side-by-side comparison has allowed end-user feedback, performance and accessibility testing. The same approach has also been useful for previewing and testing the capabilities of new features (like the new Yammer web part). The challenge comes when you need to promote or switch over one of these variations as the new site homepage. The homepage is the page users are directed to when first navigating to a site or clicking on the site logo. It is like as important as the index.html or default.aspx page existence to a website. Note that these variations of the homepage permit testing of content and not site configuration. To test navigation, theme or similar we have separate sites and environments for this purpose.

To solve the problem switching the homepage from an existing page whilst preserving the home.aspx page name I’ve leveraged the SharePoint Pnp cmdlets to create a script that will rename or remove the current homepage (and can remove it through a toggle) and then rename an existing page to make it the new homepage.

Set-SPNewHomePage script demonstration
Demonstration of the Set-SPNewHomePage script in action.

Use my PnP PowerShell script to replace the home.aspx page

Alternative methods

Change the default homepage through SharePoint

Site Owners can use the out-of-the-box make homepage action to make any page the default homepage or welcome page. This is available from the toolbar in the site pages library. But this keeps the page name and means the default page is /sitepages/randompagename.aspx instead of the standard /sitepages/home.aspx that all sites have. From my perspective this is not great. Certainly, intranet-like sites should follow some basic content management principles. Call in a touch on the OCD side but consistently having a standard homepage is one of these for me.

Screenshot of setting a new homepage in the site pages library.
Screenshot of setting a new homepage in the site pages library.

Change the welcome page site property through Site Settings or PowerShell

Previously you could use the classic settings page (typically exposed by the publishing feature) or by browsing to /_layouts/15/AreaWelcomePage.aspx to make changes to the welcome page. This method no longer works and throws an error.

As with the make homepage action describe earlier this changes the default homepage to the use the page name you have provided and means the site won’t be available if users have bookmarked the site with the page name (/sitepages/home.aspx) in the URL.

Screenshot of the welcome page site settings page.
Screenshot of the welcome page site settings page.

What is my point? To this day can I still memory recall core settings pages. With these, you can quickly review or makes changes to settings pages rather than using the UI to navigate to them. This includes those that may no longer be exposed in the UI. Whilst my memory serves me well I don’t recommend this approach as these pages and settings are gradually being replaced with alternatives or removed by the SharePoint and Office 365 engineering team for a reason.

Instead, you can also use Pnp PowerShell to change the site welcome page property. I’ve provided an example script below.

As simple as my script is, it is the approach worth learning the most. I hope you find this article useful and as with all Pnp development effort. Sharing is caring!

The Deployment Guide of all SharePoint 2013 Deployment Guides

Let me introduce you to the Deployment guide for Microsoft SharePoint 2013. Anyone deploying, installing or configuring SharePoint 2013 absolutely must read this!

This particular Deployment Guide is 674 pages long and like no other. It was published by the Microsoft Office System and Servers Team at Microsoft back in October 2012.

It is such a great guide and is packed full of information. Reading through the deployment guide, I discovered some neat little tricks and refreshed myself on some pretty important best practices which are always a good exercise.

Download it now, get reading and share it!