Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

Powershell script via GPO on Logon not working correctly

Writer Matthew Martinez

I made a Powershell script to remove the Windows Store and Cortana on Windows 10. I made and tested the Script successfully. Now i added the script to the LogonScripts on a GPO and tested the Script is running, also successfully. But it is not doing what is should do.

First of al, this is the Script:

$WSV =(Get-AppxPackage -Name "*WindowsStore*").name
if ($WSV -eq "Microsoft.WindowsStore") {Get-AppxPackage -allusers "*WindowsStore*" | Remove-AppxPackage}
$Cor =(Get-AppxPackage -Name Microsoft.549981C3F5F10).name
if ($Cor -eq "Microsoft.549981C3F5F10") {Get-AppxPackage -allusers Microsoft.549981C3F5F10 | Remove-AppxPackage}

It simply checks the existence of the AppX-Package and if it exist it will remove it. This works if the script is started by hand. But it is not working as Logon-Script. My first thought, the script is not running at all. So i added the creation of a folder in C-root, and i can confirm the script is running by the creation of this folder.

So why is the not working as desired? Is there a way get the error-message of the script somehow?

Execution-Policy is unrestricted. It is run on the Computer configuration of the GPO, so it should have Computer rights.

10

2 Answers

Run as a Startup Script rather than a Logon Script since the command is using the -AllUsers parameter to ensure the permission is sufficient to remove the packages for all users when run.

I would also put the Set-ExecutionPolicy Unrestricted in the script even though you say you have execution policy set to this already just in case.

Lastly, the removal of the package requires the full package name as far as I can tell so use the below PowerShell to get this for the app you are removing.

Use the PowerShell logic as specified in the below commands rather than what you are currently using logic wise to help resolve.

PowerShell

Set-ExecutionPolicy Unrestricted;
Get-AppxProvisionedPackage –Online | % { If($_.DisplayName -eq "Microsoft.WindowsStore") {Remove-AppxProvisionedPackage -Online -PackageName $_.PackageName} };
Get-AppxProvisionedPackage –Online | % { If($_.DisplayName -eq "Microsoft.549981C3F5F10") {Remove-AppxProvisionedPackage -Online -PackageName $_.PackageName} };
"Microsoft.WindowsStore" | %{ $p = (Get-AppxPackage -Name $_ | %{$_.PackageFullName}); If($p){Remove-AppxPackage -Package $p -AllUsers}; };
"Microsoft.549981C3F5F10" | %{ $p = (Get-AppxPackage -Name $_ | %{$_.PackageFullName}); If($p){Remove-AppxPackage -Package $p -AllUsers}; };

Supporting Resources


0

Your PowerShell code is wrong.

$WSV =(Get-AppxPackage -Name "*WindowsStore*").name
# can also be written as
$WSV = Get-AppxPackage -Name "*WindowsStore*" | Select-Object -ExpandProperty Name

This query will sometimes return an array depending on the number of elements returned:

PS > Get-Item *.html, *.log Directory: /Users/megamorf
Mode LastWriteTime Length Name
---- ------------- ------ ----
----- 06.04.2020 18:04 730 back-to-work.html
----- 05.10.2020 12:32 4269 jmeter.log
----- 28.09.2020 14:41 246873 olv.log
----- 20.10.2020 16:28 170945 web.log
PS > $Result = (Get-Item "*.log").Name
PS > $Result.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS > $Result = (Get-Item "*.html").Name
PS > $Result.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object

The comparison operator -eq is not correct for when you're dealing with arrays. In that case you want to use -in ($obj -in $array) or -contains ($array -contains $obj).

The entire code can be simplified as:

$AppsToDelete = @("Microsoft.WindowsStore", "Microsoft.549981C3F5F10")
foreach ($UnwantedApp in $AppsToDelete) { Get-AppxPackage -Name $UnwantedApp -AllUsers | Remove-AppxPackage
}
5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy