REGDB_E_CLASSNOTREG error/resolution while launching PowerShell from C# code

The following error is highlighted in this StackExchange thread and the most common solution is to ensure that PowerShell is opened from Windows Start menu and PowerShell exe must point to this path: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

What-if you get the error while launching PowerShell from C# code. This is kind of an edge case and not covered anywhere. In my situation, I launched PowerShell from my C# code as per this StackExchange answer and just for reference here is the method that invoked the PowerShell prompt/script:

//////////

 public static void LaunchPSScript(string scriptname,string workingDirectory=".")

 {

            var script = scriptname;

            var startInfo = new ProcessStartInfo()

            {

                FileName = @"powershell.exe",

                WorkingDirectory = workingDirectory,

                Arguments = $"-NoProfile -noexit -ExecutionPolicy Bypass \"{script}\"",

                UseShellExecute = false

            };

            Process.Start(startInfo);

 }

/////////

Moreover, the above was working code and started throwing the following error all of a sudden. To add to the confusion, between the last time it was working and now, I had switched over to Windows 11 Pro but unsure if the new OS was the issue. I also checked launching PowerShell from Start menu in the new Windows 11 Pro machine and the same script worked fine with the PS exe location pointing to %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe. So, in summary, Windows 11 Pro wasn't the issue. Also, I could see that the C# program launched PowerShell from the same location but error-ed in the middle as per the following screen shot.


I then toggled the CPU option in Visual Studio from Any CPU to x64:


Once I changed the CPU bitness to x64 and executed the application, the error wasn't thrown and PowerShell' Install-SitecoreConfiguration worked fine. So, if you are using C# code to launch PowerShell and get the above error, ensure that the code is compiled for x64 processor rather than "Any CPU" since Windows PowerShell functions, in this case, seem compatible only with x64 processor. I remember the last time I found this issue/resolution, I had thought it to be trivial and didn't document then but not this time! 


 [---------------------------- IdentityServer_CreateAppPool : AppPool -------------------------------------------------]

Install-SitecoreConfiguration : Cannot retrieve the dynamic parameters for the cmdlet. Retrieving the COM class factory for component with CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

+  Install-SitecoreConfiguration @singleDeveloperParams *>&1 | Tee-Obje ...

+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Install-SitecoreConfiguration


[TIME] 00:00:12

Invoke-AppPoolTask : Cannot retrieve the dynamic parameters for the cmdlet. Retrieving the COM class factory for

component with CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6} failed due to the following error: 80040154 Class not

registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

At C:\Program

Files\WindowsPowerShell\Modules\SitecoreInstallFramework\2.3.0\Public\Install-SitecoreConfiguration.ps1:641 char:25

+                         & $entry.Task.Command @paramSet | Out-Default

+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Invoke-AppPoolTask], ParameterBindingException

    + FullyQualifiedErrorId : GetDynamicParametersException,Invoke-AppPoolTask

Comments