'Powershell script work in powershell but not in c#

Below scripts format work for me in powershell to remove package from another user but its not working on C#

other script working fine in c# like get all package , remove package for all user , I run the visual studio as Admin

I am writing this in WPF application

There is no error or bug in code its just executed without error but app state not change

after running script from powershell its work fine after running script from c# application status updated as 'NeedsRemediation'

Format

Get-AppxPackage -User SID | Where PackageFullName -eq packagefullname | Remove-AppxPackage -User

Remove-AppxPackage -Package packagefullname -User SID

script

Get-AppxPackage -User S-1-5-21-1982064058-2852822003-722279830-1009 | Where PackageFullName -eq FACEBOOK.FACEBOOK_2021.927.1.0_neutral__8xx8rvfyw5nnt | Remove-AppxPackage -User S-1-5-21-1982064058-2852822003-722279830-1009

Remove-AppxPackage -Package 5319275A.WhatsAppDesktop_2.2216.8.0_x64__cv1g1gvanyjgm -User S-1-5-21-1982064058-2852822003-722279830-1009

C#

using (PowerShell PowerShellInst = PowerShell.Create())
{
    PowerShellInst.AddScript(removeCommand, false);
    var PSOutput = PowerShellInst.Invoke();
    if (PowerShellInst.Streams.Error.Count > 0)
    {
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source