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
{
$remove_appx = $Name; $provisioned = get-appxprovisionedpackage -online; $appxpackage = get-appxpackage -allusers; $eol = @()
$store = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore'
$users = @('S-1-5-18'); if (test-path $store) {$users += $((dir $store -ea 0 |where {$_ -like '*S-1-5-21*'}).PSChildName)}
foreach ($choice in $remove_appx) { if ('' -eq $choice.Trim()) {continue}
foreach ($appx in $($provisioned |where {$_.PackageName -like "*$choice*"})) {
$next = !1; foreach ($no in $skip) {if ($appx.PackageName -like "*$no*") {$next = !0}} ; if ($next) {continue}
$PackageName = $appx.PackageName; $PackageFamilyName = ($appxpackage |where {$_.Name -eq $appx.DisplayName}).PackageFamilyName
ni "$store\Deprovisioned\$PackageFamilyName" -force >''; $PackageFamilyName
foreach ($sid in $users) {ni "$store\EndOfLife\$sid\$PackageName" -force >''} ; $eol += $PackageName
dism /online /set-nonremovableapppolicy /packagefamily:$PackageFamilyName /nonremovable:0 >''
remove-appxprovisionedpackage -packagename $PackageName -online -allusers >''
}
foreach ($appx in $($appxpackage |where {$_.PackageFullName -like "*$choice*"})) {
$next = !1; foreach ($no in $skip) {if ($appx.PackageFullName -like "*$no*") {$next = !0}} ; if ($next) {continue}
$PackageFullName = $appx.PackageFullName;
ni "$store\Deprovisioned\$appx.PackageFamilyName" -force >''
foreach ($sid in $users) {ni "$store\EndOfLife\$sid\$PackageFullName" -force >''} ; $eol += $PackageFullName
dism /online /set-nonremovableapppolicy /packagefamily:$PackageFamilyName /nonremovable:0 >''
remove-appxpackage -package $PackageFullName -allusers >''
}
}
}
}
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" )
$szBackFilePath = "$($szCBSXMLPath)_$($szCurrentTime).xml"
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
}
Write-Host -ForegroundColor White "Restoring the original AppX manifest file..."
Move-Item -Force -Path $szBackFilePath -Destination $szCBSXMLPath
}
}
Remove_StartMenuCBSItems