Skip to content

Instantly share code, notes, and snippets.

@danmaq
Created January 12, 2017 20:53
Show Gist options
  • Save danmaq/27256391a70beb4e451d9bcc055e7bf7 to your computer and use it in GitHub Desktop.
Save danmaq/27256391a70beb4e451d9bcc055e7bf7 to your computer and use it in GitHub Desktop.
Script of upgrade powershell to 5.0. Targets: Windows 7 or 8.1. (2008 R2 or 2012 R2 are not supported yet)
@powershell -NoProfile -ExecutionPolicy Unrestricted "$s=[scriptblock]::create((gc \"%~f0\"|?{$_.readcount -gt 1})-join\"`n\");&$s" %*&goto:eof
Set-StrictMode -Version 2.0
# OS およびバージョン検証 (使用可能な OS は Windows7 または 8.1 の x86 または x64 のみ)
$os = [System.Environment]::OSVersion
if ($os.Platform -ne [System.PlatformID]::Win32NT) { throw "Detected the OS type that is not supported. This installer requires Windows 7 (x86 or x64) or upper version." }
$wver = $os.Version
if ($wver.Major -lt 6 -or $wver.Minor -lt 1) { throw "Detected the OS version that is not supported. This installer requires Windows 7 (x86 or x64) or upper version." }
if ($wver.Major -eq 6 -and $wver.Minor -eq 2) { throw "Windows 8 should be upgraded to 8.1 before this installation." }
# Powershell のバージョン取得
if ($PSVersionTable.PSVersion.Major -lt 5)
{
$x64 = (Get-WmiObject -Class Win32_OperatingSystem).OSarchitecture -match "64"
$w7 = $wver.Minor -eq 1
$fname = $null
if ($x64 -and $w7) { $fname = "Win7AndW2K8R2-KB3134760-x64" }
elseif (-not $x64 -and $w7) { $fname = "Win7-KB3134760-x86" }
elseif (-not $x64 -and -not $w7) { $fname = "Win8.1-KB3134758-x86" }
else { $fname = "Win8.1AndW2K12R2-KB3134758-x64" }
if ($fname -eq $null) { throw "Detected the OS version that is not supported. This installer requires Windows 7 (x86 or x64) or upper version." }
$file = $fname + ".msu"
$uri = "https://download.microsoft.com/download/2/C/6/2C6E1B4A-EBE5-48A6-B225-2D2058A9CEFB/" + $file
#echo "Downloading WMF 5..."
#echo $uri
(new-object System.Net.WebClient).Downloadfile($uri, $file)
#echo "Start install. (Reboot after installed)"
wusa $file /quiet /forcerestart
}
@danmaq
Copy link
Author

danmaq commented Jan 12, 2017

Note: This script require run as Administrator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment