Search code examples
powershellregistry

Powershell edit HKLM registry with alternate credentials


Trying to use Set-ItemProperty to set a HKLM registry using alternate admin credentials, but I am getting an error, "The provider does not support the use of credentials." while running this script as a standard end user who will not have write access to the HKLM value we want to edit.

$RegKey1 ='HKLM:\SOFTWARE\Microsoft\Office\Outlook\Addins\Workshare.Client.OutlookFormUI.AddinModule'

$username = "LocalAdmin"
$password = "Passw0rd"
$AdminCred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))

Set-ItemProperty -Path $RegKey1 -Name "LoadBehavior" -Value 2 -Credential $AdminCred

Solution

  • The solution was to create a reg file with all the desired HKLM changes in it and use start-process with -credentials parameter to execute reg.exe.

    $username = "mydomain\localdmin"
    $password = "Passw0rd"
    $AdminCred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
    $regFile = "\\myServer\myShare\myRegFile.reg"
    $regArg1 = "import $regFile"
    
    Start-Process reg.exe -ArgumentList $regArg1 -Credential $AdminCred