Search code examples
windowspowershellshellregistryacl

How to write to registry from script when user is able but script not?


I have a need to amend values below the following registry key: HKLM:\software\microsoft\Windows\CurrentVersion\MMDevices\Audio\Render Namely the 'DeviceState' value at startup/logon. Windows 10 machines.

Ive written a powershell script to do what I need but it always gets a "Registry Access is not allowed" Error.

If I open Regedit and browse manually to the desired value I can change it no problem, run the script, not allowed.

Ive tried the script as a scheduled task to run at startup and still no joy. As we build machines from scratch Ive written an extra installation bit to try and set the permissions on the above key so that scripts can write to it. Setting the ACL from that returns the same error, "not allowed".

Anyone know how I can make the changes I need from a script?

As a footnote the above reg key and value are what the "sounds" (right click speaker icon task bar) ->Playback tab writes to as you change the state of the devices shown there. I need to set one of them to always be the default and disable the others. There are 5 devices shown in my setup.

Thanks


Solution

  • Ok In the end I found this which seems to be working. I needed to take ownership of a protected key in the registry and then do some acl tweaking.

    How do I take ownership of a registry key via PowerShell?

    'argimko's answer and function.