Skip to content

Instantly share code, notes, and snippets.

@aplocher
Created February 20, 2015 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aplocher/b6012f07615ecdbb9b8b to your computer and use it in GitHub Desktop.
Save aplocher/b6012f07615ecdbb9b8b to your computer and use it in GitHub Desktop.
# Tested with Windows 10 Tech Preview build 9926
#
# The following PowerShell script can be used to re-initialize the Shell Experience in Windows 10.
#
# WARNING: All store apps will be reinitialized meaning any local data associated with that app will
# get reset.
#
# This seems to be necessary if restoring from a restore point. When a restore point is used,
# the Start Menu, Cortana, and the Notification Area no longer work
param (
[switch]$IsRunAsAdmin = $false
)
$ScriptPath = (Get-Variable MyInvocation).Value.MyCommand.Path
function BeforeStart {
Stop-Service 'wuauserv'
}
function AfterFinish {
Start-Service 'wuauserv'
}
function RepairStep1 {
Write-Host 'Step 1 of 2 starting in 5 seconds (press any key to cancel...)'
$canceled = KeyPressOrSleep
if ($canceled) {
Write-Host 'Canceled'
return $false
}
Write-Host 'Starting repair step 1...'
Get-AppXPackage -AllUsers *shellexperience* -PackageType bundle |% {Add-AppxPackage -DisableDevelopmentMode -Register ($_.installlocation + "\appxmetadata\AppXBundleManifest.xml")}
Write-Host 'Done'
return $true
}
function RepairStep2 {
Write-Host 'Starting repair step 2 of 2 in 5 seconds (press any key to cancel...)'
Write-Host 'Note: don''t worry if you see a few errors here...'
$canceled = KeyPressOrSleep
if ($canceled) {
Write-Host 'Canceled'
return $false
}
Write-Host 'Starting repair step 2...'
Get-AppXPackage -AllUsers |% {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}
Write-Host 'Done (a few errors are normal)'
return $true
}
function KeyPressOrSleep {
$counter = 0
while(!$Host.UI.RawUI.KeyAvailable -and ($counter++ -lt 20)) {
Start-Sleep -Milliseconds 250
if ($Host.UI.RawUI.KeyAvailable) {
$Host.UI.RawUI.FlushInputBuffer()
return $true
}
}
return $false
}
function StartOperation() {
BeforeStart
try {
$success = RepairStep1
if ($success) {
$null = RepairStep2
Write-Host 'Exiting in 5 seconds (or press any key)'
$null = KeyPressOrSleep
}
} finally {
AfterFinish
}
}
#################################################
# Everything below is boiler plate code for #
# re-executing self with "Run As Admin" enabled #
#################################################
function RelaunchProcessElevated {
try {
$RelaunchArgs = '-ExecutionPolicy Unrestricted -file "' + $ScriptPath + '" -IsRunAsAdmin'
$AdminProcess = Start-Process "$PsHome\PowerShell.exe" -Verb RunAs -ArgumentList $RelaunchArgs -PassThru
return $AdminProcess
} catch {
$Error[0] # Dump details about the last error
exit 1
}
}
function RunAsAdmin {
if (-not $IsRunAsAdmin -and ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
$IsRunAsAdmin = $true
}
if ($IsRunAsAdmin) {
Write-Host 'Starting Operation'
StartOperation
} else {
Write-Host 'Elevating'
$AdminProcess = RelaunchProcessElevated
while (-not $AdminProcess.HasExited) {
Start-Sleep -Seconds 2
}
}
}
RunAsAdmin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment