2

I create a new user via PowerShell, then remove unnecessary apps for that user. However, the new user must log in at least once before any apps can be removed, otherwise I get:

The security ID structure is invalid.

How can I remove these apps for a new user without login?

function removeAppx {
    param([string]$Username)

    $Apps = @(
        "Microsoft.549981C3F5F10",
        "Microsoft.BingNews",
        "Microsoft.BingWeather",
        "Microsoft.GamingApp",
        "Microsoft.GetHelp",
        "Microsoft.Getstarted",
        "Microsoft.MicrosoftEdge.Stable",
        "Microsoft.MicrosoftOfficeHub",
        "Microsoft.MicrosoftSolitaireCollection",
        "Microsoft.MicrosoftStickyNotes",
        "Microsoft.Paint",
        "Microsoft.People",
        "Microsoft.PowerAutomateDesktop",
        "Microsoft.ScreenSketch",
        "Microsoft.StorePurchaseApp",
        "Microsoft.Todos",
        "Microsoft.Windows.Photos",
        "Microsoft.WindowsAlarms",
        "Microsoft.WindowsCalculator",
        "Microsoft.WindowsCommunicationsApps",
        "Microsoft.WindowsFeedbackHub",
        "Microsoft.WindowsMaps",
        "Microsoft.WindowsNotepad",
        "Microsoft.WindowsSoundRecorder",
        "Microsoft.Xbox.TCUI",
        "Microsoft.XboxGameOverlay",
        "Microsoft.XboxGamingOverlay",
        "Microsoft.XboxIdentityProvider",
        "Microsoft.XboxSpeechToTextOverlay",
        "Microsoft.YourPhone",
        "Microsoft.ZuneMusic",
        "Microsoft.ZuneVideo",
        "MicrosoftTeams",
        "MicrosoftWindows.Client.WebExperience"
    )

    foreach ($App in $Apps) {
        try {
            $Package = Get-AppxPackage -Name $App -User "test" -ErrorAction SilentlyContinue
            if ($null -ne $Package) {
                Write-Host "[DEL] $App (utilisateur $Username)" -NoNewline -ForegroundColor Green
                Remove-AppxPackage -Package $Package.PackageFullName -User $Username -ErrorAction Stop
                Write-Host " [OK]" -ForegroundColor Green
            }
        } catch { Write-Host "Erreur: $_" -ForegroundColor Red }

        try {
            $ProvPackage = Get-AppxProvisionedPackage -Online | Where-Object {$_.DisplayName -eq $App}
            if ($null -ne $ProvPackage) {
                Write-Host "[DEL] $App" -NoNewline -ForegroundColor Yellow
                Remove-AppxProvisionedPackage -Online -PackageName $ProvPackage.PackageName -ErrorAction Stop
                Write-Host " [OK]" -ForegroundColor Yellow
            }
        } catch { Write-Host "Erreur provisioned: $_" -ForegroundColor Red }
    }

    Write-Host "`nSuppression terminée pour l'utilisateur $Username." -ForegroundColor Green

}

error

6
  • Perhaps the following helps: learn.microsoft.com/en-us/previous-versions/troubleshoot/… Commented Oct 16 at 10:56
  • 1
    may be running a task under that user credentials or impersonating as the user? but, if the user account has never been used, there is no user environment, so probably these may not work... Commented Oct 16 at 11:23
  • @elzooilogico i'll try it. also, i found that : learn.microsoft.com/en-us/windows/configuration/… Commented Oct 16 at 11:42
  • 6
    register the script to be run on first login, or on every login with self-removal so it only happens once Commented Oct 16 at 13:14
  • 2
    FWIW, I would leave the store app, since certain parts of Windows now use it internally for servicing updates (notepad, calculator, and a number of other actually useful tools). Instead, there are group policy, local policy, and registry key options to prevent users from opening or otherwise using the store interface, and even hide it from the start menu/taskbar. Also: you block Notepad? Really? Commented Oct 31 at 21:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.