Hey everyone! I'm trying to work with PowerShell COM objects, but I keep running into an issue. I'm using the command `New-Object -ComObject`, and I've tried several options like `PowerShell.Application`, `System.Management.Automation.PowerShell`, and `Microsoft.PowerShell.Utility`, but I keep getting an error stating that the COM class factory for the specified CLSID failed because it's not registered. I'm a bit stuck here.
I need to create a PowerShell COM object because I have another application that can leverage these COM objects, and I want to manipulate the PowerShell instance through that app. I have some old code that worked before, but unfortunately, it's on another machine far away, and I can't access it just now.
After reaching out to a friend, they mentioned that I might have used `PowerShell.Application` successfully in the past. I wonder, could it be that this class is only compatible with older versions of Windows? The machine ran Windows 7 before upgrading to Windows 10. Any suggestions on how I can find the right COM object name would be greatly appreciated!
1 Answer
It sounds like you're having a tough time! Just a heads up, when you're referring to PowerShell COM objects, you might be a bit confused. If you're looking to create an instance of PowerShell, you're actually better off using the .NET type instead of a COM object. You can create a new PowerShell instance with `PowerShell::Create()` like this:
```powershell
$ps = [PowerShell]::Create()
```
This is the recommended approach. Also, if you're running into that CLSID error, that usually means the COM object doesn't exist, so double-checking the ProgId for PowerShell might help.
Thanks, I will give this a try tomorrow!