Function Restart-Process
{
Param( [Parameter(ValueFromPipelineByPropertyName)]$Name,
[Parameter(ValueFromPipelineByPropertyName)]$Wait,
[Parameter(ValueFromPipelineByPropertyName)]$Timeout,
[Parameter(ValueFromPipelineByPropertyName)]$Kill,
[Parameter(ValueFromPipelineByPropertyName)]$Start )
Process
{
If ( $Kill )
{
$szSystem32Path = [Environment]::GetFolderPath("System")
$szTaskKillBinary = "$szSystem32Path\taskkill.exe"
Write-Host -ForegroundColor White "Killing the process '$Name'..."
# Note, stopping Explorer like this will cause it to not automatically restart
Start-Process -WindowStyle Hidden -Wait -FilePath $szTaskKillBinary -ArgumentList "/F /IM $Name"
}
Else
{
Write-Host -ForegroundColor White "Stopping the process '$Name'..."
Stop-Process -Name $Name -Force # Note, stopping Explorer like this will cause it to automatically restart
Write-Host -ForegroundColor White "Waiting for the process '$Name' to stop..."
Wait-Process -Name $Name -Timeout $Timeout
}
If ( $Start )
{
Write-Host -ForegroundColor White "Starting the process '$Name'..."
If ( $Name -Eq "explorer.exe" )
{
Start-Process -WindowStyle Hidden -FilePath "cmd" -ArgumentList "/c start /wait $Name"
}
Else
{
# Wait doesn't work on explorer
Start-Process -WindowStyle Hidden -Wait -FilePath $Name
}
}
$szNameWithoutExtension = [io.path]::GetFilenameWithoutExtension( $Name )
Write-Host -ForegroundColor White "Waiting for the process '$szNameWithoutExtension' to start..."
$iCount = 0
While ( ((Get-Process -Name $szNameWithoutExtension -ErrorAction SilentlyContinue).Count -Eq 0) -And ($iCount -lt $Timeout) )
{
Start-Sleep -Seconds 1
$iCount++
}
Write-Host -ForegroundColor White "Waiting $Wait seconds..."
Start-Sleep -Seconds $Wait
}
}
Function Remove_NonRemovablePackage
{
Param( [Parameter(ValueFromPipelineByPropertyName)]$Name )
Process
{
Write-Host -ForegroundColor White "Getting details for package '$($objAppx.PackageFamilyName)'..."
$objAppx = Get-AppxPackage -AllUsers -Name $Name
$szStoreRegKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore"
$aszUserSIDs = @( "S-1-5-18" )
If ( Test-Path $szStoreRegKey )
{
$aszUserSIDs += $((Get-ChildItem -Path $szStoreRegKey -ErrorAction SilentlyContinue | Where {$_ -Like '*S-1-5-21*'}).PSChildName)
}
Write-Host -ForegroundColor White "Setting package '$($objAppx.PackageFamilyName)' to Deprovisioned..."
$objDeprovisionedRegKey = New-Item -Force -Path "$szStoreRegKey\Deprovisioned\$($objAppx.PackageFamilyName)"
ForEach ( $szUserSID in $aszUserSIDs )
{
Write-Host -ForegroundColor White "Setting package '$($objAppx.PackageFamilyName)' to End-of-life for user SID '$szUserSID'..."
$objEOLRegKey = New-Item -Force -Path "$szStoreRegKey\EndOfLife\$szUserSID\$($objAppx.PackageFullName)"
}
Write-Host -ForegroundColor White "Removing the NonRemovable flag from package '$($objAppx.PackageFamilyName)'..."
DISM /Online /Set-NonRemovableAppPolicy /PackageFamily:$($objAppx.PackageFamilyName) /NonRemovable:0 | Out-Null
Write-Host -ForegroundColor White "Removing package '$($objAppx.PackageFamilyName)'..."
$objRemovedPackage = Remove-AppxPackage -AllUsers -Package $objAppx.PackageFullName
}
}
Function Remove_StartMenuCBSItems
{
Param( [Parameter(ValueFromPipelineByPropertyName)]$Reinstall = $True,
[Parameter(ValueFromPipelineByPropertyName)]$RestartExplorer = $True )
Process
{
$szCBSName = "MicrosoftWindows.Client.CBS"
$szCBSAppName = "$($szCBSName)_cw5n1h2txyewy"
$szWindowsPath = [Environment]::GetFolderPath( "Windows" )
$szCBSXMLPath = "$szWindowsPath\SystemApps\$($szCBSAppName)\appxmanifest.xml"
Write-Host -ForegroundColor White "Creating a backup of the Appx manifest file for '$szCBSAppName'..."
$szCurrentTime = (Get-Date).ToString( "yyyy-MM-dd_HH-mm-ss" )
Copy-Item -Force -Path $szCBSXMLPath -Destination "$($szCBSXMLPath)_$($szCurrentTime).xml"
Write-Host -ForegroundColor White "Reading the Appx manifest file for '$szCBSAppName'..."
$xmlCBS = [XML]( Get-Content $szCBSXMLPath )
Write-Host -ForegroundColor White "Removing the Start menu item 'Get Started'..."
$xmlCBSNodes = $xmlCBS.Package.Applications.Application | Where-Object { $_.Id -Eq "WebExperienceHost" }
$xmlCBSNodes.VisualElements.SetAttribute( "AppListEntry", "none" )
Write-Host -ForegroundColor White "Removing the Start menu item 'Windows Back up'..."
$xmlCBSNodes = $xmlCBS.Package.Applications.Application | Where-Object { $_.Id -Eq "WindowsBackup" }
$xmlCBSNodes.VisualElements.SetAttribute( "AppListEntry", "none" )
Write-Host -ForegroundColor White "Removing the Start menu item 'Continue from Phone'..."
$xmlCBSNodes = $xmlCBS.Package.Applications.Application | Where-Object { $_.Id -Eq "CrossDeviceResumeApp" }
$xmlCBSNodes.VisualElements.SetAttribute( "AppListEntry", "none" )
$szUser = "Administrators"
Write-Host -ForegroundColor White "Setting '$szUser' to have full control of '$szCBSXMLPath'..."
$arPath = New-Object System.Security.AccessControl.FileSystemAccessRule( $szUser, "FullControl", $([System.Security.AccessControl.InheritanceFlags]::None), "None", "Allow" )
$aclPath = $aclOrig = Get-Acl $szCBSXMLPath
$aclPath.SetAccessRule( $arPath )
Set-Acl $szCBSXMLPath $aclPath
Write-Host -ForegroundColor White "Saving the updated Appx manifest file for '$szCBSAppName'..."
$objUTF8WithoutBOM = New-Object System.Text.UTF8Encoding( $False )
$objUTF8WithoutBOMSW = New-Object System.IO.StreamWriter( $szCBSXMLPath, $False, $objUTF8WithoutBOM )
$xmlCBS.Save( $objUTF8WithoutBOMSW )
$objUTF8WithoutBOMSW.Close()
Write-Host -ForegroundColor White "Resetting permissions on '$szCBSXMLPath'..."
Set-Acl $szCBSXMLPath $aclOrig
If ( $Reinstall )
{
Remove_NonRemovablePackage -Name $szCBSName
Write-Host -ForegroundColor White "Installing package '$szCBSAppName'..."
Add-AppxPackage -ForceApplicationShutdown -DisableDevelopmentMode -Register $szCBSXMLPath
}
If ( $RestartExplorer )
{
Restart-Process -Name "explorer.exe" -Wait 0 -Timeout 5 -Kill:$True -Start:$True
}
}
}
Remove_StartMenuCBSItems