When running Get-WinGetPackage in a Windows Service context returns error This cmdlet is not supported in Windows PowerShell. However, if not ran in Service context, runs perfectly. Why?
Brief description of your issue
I have a rust program that can run as a normal console program or as a Windows Service and I need to run this command "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -Command { Get-WinGetPackage | ConvertTo-Json }".
When I run the rust program as a normal console program everything works perfectly and I get the data I need, as well as if I run that command directly in the console.
However, if I run that same rust program as a Windows Service it gives this error: Get-WinGetPackage: This cmdlet is not supported in Windows PowerShell.
It is using the same powershell path but I receive different responses. Why??
I have only tested this in a VM environment, i used Windows 11 Pro.
Thank you for your time.
Steps to reproduce
Create a rust program that executes this command "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -Command { Get-WinGetPackage | ConvertTo-Json }", and run it as a normal console program and as a Windows Service.
Expected behavior
The command should return a json containing information about the winget packages (Get-WinGetPackage)
Actual behavior
The command returns this error: Get-WinGetPackage: This cmdlet is not supported in Windows PowerShell.
Environment
Windows 11 Pro
Rust Program
Windows Service
This is most likely because the service is attempting to run using the System context instead of a user context
There are frequent issues with the System context and Windows PowerShell. Many of these issues are not present on PowerShell 7.0 - is it possible to install the latter?
Thank you for both answers. It is not possible to install powershell 7.0 for this. I actually have in complement a system tray app so I think I will need to execute the command in there. Or is there any more solutions to this?