Published in Articles on by Michiel van Oosterhout ~ 2 min read
The Control Panel has mostly been superseded by the Settings app in Windows 11. To automate the configuration of Windows using Windows PowerShell we need to find the corresponding registry values. The Windows PowerShell function below can change Windows settings found in the Settings app.
(This function may require an elevated Windows PowerShell session.)
Function ProcessSettings()
{
param(
[Parameter(Mandatory)]
[Array]$Nodes,
[Parameter(Mandatory)]
[int]$Indent
)
# Loop over all child nodes
foreach ($node in $Nodes)
{
# Print node (either a name or a setting and its value
Write-Host "$(" " * $Indent)$($node.Name)"
if ($node.Registry -is [Hashtable])
{
$path = $node.Registry.Path
# Ensure the key exists
if (-not (Test-Path $path))
{
$key = New-Item -Force -ItemType Directory -Path $path
}
# Determine the value type
$type = "DWord"
$value = $node.Registry.Value
if ($value -is [bool])
{
$value = [int]$value
}
elseif ($value -is [string])
{
$type = "String"
}
# Set the value
$value = New-ItemProperty -Force -Name $node.Registry.Name -Path $path -PropertyType $type -Value $value
}
if ($Node.Nodes -is [Array])
{
ProcessSettings -Nodes $Node.Nodes -Indent ($Indent + 4)
}
}
}
The function has a parameter named Nodes
that accepts an array. Each entry in the array should be a hash table with a Name
property, and optional Registry
and Nodes
properties. The Nodes
property must be a similar array. This allows the parameter value to mirror the hierarchical structure of the Settings app. The function ProcessSettings
is a recursive function that visits each node in the tree and if it finds a node with a Registry
property it will use it to set a value in the Windows Registry.
An example value for the Nodes
parameter, corresponding to the System > Notifications section of the Settings app, is given below:
@(
@{
Name = "Notifications: On"
Registry = @{
Path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\PushNotifications"
Name = "ToastEnabled"
Value = $true
}
Nodes = @(
@{
Name = "Allow notifications to play sound: No"
Registry = @{
Path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Notifications\Settings"
Name = "NOC_GLOBAL_SETTING_ALLOW_NOTIFICATION_SOUND"
Value = $false
}
}
)
}
)
Summary
We can use Windows PowerShell to configure Windows 11 settings found in the Settings app. With a recursive function we can maintain the settings in a hierarchical data structure.