Search code examples
vbapowershellregistry

Set-ItemProperty can't find path when using CreateProcess and Powershell Start-Process in VBA


I need to change a reg keys from a VBA script using CreateProcess and Powershell. Following you will find my VBA code.

When I run the code inside the quotes directly in Powershell or using windows run, the registry key gets set like it should. When I use this VBA code I get an path error.

The path is absolutely correct. In the same VBA script I set some other reg keys using the same command with no errors. I am out of ideas about what the mistake might be. I tested the script on two fresh Windows 10 VMs with the same result.

Thank you in advance for any help.

Dim pInfo As PROCESS_INFORMATION
Dim sInfo As STARTUPINFO
Dim sNull As String
Dim lSuccess As Long
Dim lRetValue As Long

lSuccess = CreateProcess(sNull, "powershell start-process -filepath powershell.exe -Argumentlist 'Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows` Defender` Security` Center\Notifications -Name DisableNotifications -Value 1' -verb RunAs -windowstyle hidden -Wait", ByVal 0&, ByVal 0&, 1&, CREATE_NO_WINDOW, ByVal 0&, sNull, sInfo, pInfo)
Set-ItemProperty : Der Pfad "HKLM:\SOFTWARE\Microsoft\Windows Defender Security Center\Notifications" kann nicht
gefunden werden, da er nicht vorhanden ist.
In C:\Users\public\psc.ps1:9 Zeichen:1
+ Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows` Defender` Se ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (HKLM:\SOFTWARE\...r\Notifications:String) [Set-ItemProperty], ItemNotFo
   undException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetItemPropertyCommand


Note---------------------------------------
(The german stuff at the beginning means the following)
The path "HKLM:\SOFTWARE\Microsoft\Windows Defender Security Center\Notifications" cannot be found, because it does not exist"

EDIT1:

Surprisingly the reg key is being set on my development vm when I run the script there which I found out just now. So I would assume a problem with Windows or installed software. I already tested all differences I can think of, but no luck


Solution

  • EDIT1: So I finally found the real solution and reason for this problem. As stated in the old "solution" there is a problem between 32-bit and 64-bit version of Office. I tried to set the registry value using reg add and found out, it writes to WOW6432 a compatibility layer for 32-bit applications. Which is pretty obvious if you know that that exists because Office is usually a 32-bit application. I assume the path could not be found because powershell is running in the context of 32-bit Office and tries to access the 64-bit part of the registry. The reg add command sets the value in WOW6432, which won't work, because Windows Defender Security Center doesn't read the values there. But there is a /reg:64 flag, which forces reg add to write to the 64-bit part of the registry. And everything works just fine like it should.