Skip to content

Instantly share code, notes, and snippets.

@dimon4ezzz
Last active December 16, 2022 02:02
Show Gist options
  • Save dimon4ezzz/3beefe720c6aa216abf491653ea732eb to your computer and use it in GitHub Desktop.
Save dimon4ezzz/3beefe720c6aa216abf491653ea732eb to your computer and use it in GitHub Desktop.
Shell script for Windows XP installing on Windows 10 from official Microsoft download center.
#Requires -version 4.0
#Requires –RunAsAdministrator
[CmdletBinding()]
param (
[Parameter(Mandatory = $false, HelpMessage = "Choose custom language and version. For example, 'zh-TW' or 'N_de-de'. By default this parameter is host system language.")]
[string]
$Locale = ((Get-WinSystemLocale).Name).ToLower(),
[Parameter(Mandatory = $false, HelpMessage = "Path to 'WindowsXPMode_xx-xx.exe' file.")]
[ValidateScript({Test-Path $_})]
[string]
$XPModeFile,
[Parameter(Mandatory = $false)]
[ValidateScript({Test-Path $_})]
[string]
$TempDirectory = $env:TEMP,
[Parameter(Mandatory = $false)]
[ValidateScript({Test-Path $_})]
[string]
$VMDirectory
)
$BaseURI = "https://download.microsoft.com/download/"
$Paths = @{
"ar-sa" = "2/5/B/25B89F73-F3D6-44AC-8E53-44473B508093/WindowsXPMode_ar-sa.exe";
"cs-cz" = "9/7/5/975C7E04-C541-4895-9320-79A6CA398EB9/WindowsXPMode_cs-cz.exe";
"n_cs-cz" = "9/7/5/975C7E04-C541-4895-9320-79A6CA398EB9/WindowsXPMode_N_cs-cz.exe";
"da-dk" = "1/9/B/19B4BC03-EEC6-483C-AAD9-FDC71391D9C0/WindowsXPMode_da-dk.exe";
"n_da-dk" = "1/9/B/19B4BC03-EEC6-483C-AAD9-FDC71391D9C0/WindowsXPMode_N_da-dk.exe";
"de-de" = "C/9/8/C984738B-C8C4-4A59-B396-DD77606D7932/WindowsXPMode_de-de.exe";
"n_de-de" = "C/9/8/C984738B-C8C4-4A59-B396-DD77606D7932/WindowsXPMode_N_de-de.exe";
"el-gr" = "F/7/5/F75B1D75-7A1F-4AC1-9788-0F970FC7D875/WindowsXPMode_el-gr.exe";
"n_el-gr" = "F/7/5/F75B1D75-7A1F-4AC1-9788-0F970FC7D875/WindowsXPMode_N_el-gr.exe";
"en-us" = "7/2/C/72C7BAB7-2F32-4530-878A-292C20E1845A/WindowsXPMode_en-us.exe";
"n_en-us" = "7/2/C/72C7BAB7-2F32-4530-878A-292C20E1845A/WindowsXPMode_N_en-us.exe";
"es-es" = "8/A/A/8AA64E91-E64C-45B9-B973-2BC25DE1F5D7/WindowsXPMode_es-es.exe";
"n_es-es" = "8/A/A/8AA64E91-E64C-45B9-B973-2BC25DE1F5D7/WindowsXPMode_N_es-es.exe";
"fi-fi" = "3/4/B/34BB5FDD-8F9E-4AB8-8D16-4D90050388A6/WindowsXPMode_fi-fi.exe";
"n_fi-fi" = "3/4/B/34BB5FDD-8F9E-4AB8-8D16-4D90050388A6/WindowsXPMode_N_fi-fi.exe";
"fr-fr" = "0/3/A/03A77CAF-EF08-4BE1-8B56-A3E615CD4FE0/WindowsXPMode_fr-fr.exe";
"n_fr-fr" = "0/3/A/03A77CAF-EF08-4BE1-8B56-A3E615CD4FE0/WindowsXPMode_N_fr-fr.exe";
"he-il" = "D/6/1/D61356B3-2C1F-47F9-8EA0-DF3F2023018D/WindowsXPMode_he-il.exe";
"hu-hu" = "A/F/6/AF6B89E4-50FB-4065-8105-106D5339C9BC/WindowsXPMode_hu-hu.exe";
"n_hu-hu" = "A/F/6/AF6B89E4-50FB-4065-8105-106D5339C9BC/WindowsXPMode_N_hu-hu.exe";
"it-it" = "E/5/6/E5609BB4-951F-42B8-ACCB-98266C0A6AF6/WindowsXPMode_it-it.exe";
"n_it-it" = "E/5/6/E5609BB4-951F-42B8-ACCB-98266C0A6AF6/WindowsXPMode_N_it-it.exe";
"ja-jp" = "A/2/F/A2F07A26-83C8-41FD-BD1C-76E4ECB4839F/WindowsXPMode_ja-jp.exe";
"ko-kr" = "A/3/C/A3CCFA1E-2B05-4C46-BB08-C6B74C861385/WindowsXPMode_ko-kr.exe";
"k_ko-kr" = "A/3/C/A3CCFA1E-2B05-4C46-BB08-C6B74C861385/WindowsXPMode_K_ko-kr.exe";
"kn_ko-kr" = "A/3/C/A3CCFA1E-2B05-4C46-BB08-C6B74C861385/WindowsXPMode_KN_ko-kr.exe";
"nb-no" = "0/A/3/0A3F3143-0398-4EC7-B5A1-AB93C7F92677/WindowsXPMode_nb-no.exe";
"n_nb-no" = "0/A/3/0A3F3143-0398-4EC7-B5A1-AB93C7F92677/WindowsXPMode_N_nb-no.exe";
"nl-nl" = "D/4/2/D42599F3-B413-4BFA-BE57-A61128B169C5/WindowsXPMode_nl-nl.exe";
"n_nl-nl" = "D/4/2/D42599F3-B413-4BFA-BE57-A61128B169C5/WindowsXPMode_N_nl-nl.exe";
"pl-pl" = "9/2/7/9270EA23-97C9-4142-A1B2-73AFBAE66B39/WindowsXPMode_pl-pl.exe";
"n_pl-pl" = "9/2/7/9270EA23-97C9-4142-A1B2-73AFBAE66B39/WindowsXPMode_N_pl-pl.exe";
"pt-br" = "2/8/6/286E425C-E45B-4132-A775-78C1AE1B79C4/WindowsXPMode_pt-br.exe";
"pt-pt" = "8/9/4/894F40AD-F959-424D-AD83-51C63EB2BB7C/WindowsXPMode_pt-pt.exe";
"n_pt-pt" = "8/9/4/894F40AD-F959-424D-AD83-51C63EB2BB7C/WindowsXPMode_N_pt-pt.exe";
"ru-ru" = "7/5/B/75B73FA4-3ACB-4131-8A32-B3E51CC3FEF5/WindowsXPMode_ru-ru.exe";
"sv-se" = "C/3/3/C3345B63-480E-4DC9-AA9E-C1B5D3341690/WindowsXPMode_sv-se.exe";
"n_sv-se" = "C/3/3/C3345B63-480E-4DC9-AA9E-C1B5D3341690/WindowsXPMode_N_sv-se.exe";
"tr-tr" = "0/9/E/09E48784-0258-4390-8CB5-6C1A2DD7D537/WindowsXPMode_tr-tr.exe";
"zh-cn" = "D/7/A/D7AD3FF8-2618-4C10-9398-2810DDE730F7/WindowsXPMode_zh-cn.exe";
"zh-hk" = "A/B/C/ABC59DE9-D68C-4885-A962-355FFEB15F6C/WindowsXPMode_zh-hk.exe";
"zh-tw" = "B/9/3/B93CD319-CD5A-41C8-9577-39F68D5E8009/WindowsXPMode_zh-tw.exe";
}
function CheckWindowsVersion {
Write-Verbose "Checking Windows version."
$ThisVersion = ([System.Environment]::OSVersion.Version).Major
if ($ThisVersion -ne 10) {
Write-Host "This script can install Windows XP Virtual Machine on Windows 10 only."
Exit-PSHostProcess
}
Write-Verbose "Windows is suitable."
}
function CheckWindowsEdition {
Write-Verbose "Checking Windows edition."
$Edition = (Get-WindowsEdition -Online).Edition
if (
$Edition -ne "Enterprise" -and
$Edition -ne "Pro" -and
$Edition -ne "Education"
) {
Write-Host "Cannot install Hyper-V cause of Windows 10 edition is $Edition."
Exit-PSHostProcess
}
Write-Verbose "Windows edition is suitable."
}
function CheckHyperVEnabled {
$HyperVStatus = (Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online).State
if ($HyperVStatus -ne "Enabled") {
Write-Verbose "Hyper-V is not enabled. Enabling..."
Enable-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V -Online -All
}
}
function CheckWindowsXPAvaliablitily {
Write-Verbose "Checking Windows XP locale avaliablility."
if (!$Paths[$Locale]) {
Write-Verbose "Cannot find package for this locale: $Locale. Using 'en-us' instead."
$Locale = "en-us"
} else {
Write-Verbose "Found Windows XP for $Locale."
}
}
function CheckInternetConnection {
Write-Verbose "Checking internet connection..."
if (!((Test-NetConnection -ComputerName "microsoft.com" -Port 443).TcpTestSucceeded)) {
Write-Host "Cannot connect to Microsoft server."
Exit-PSSession
}
Write-Verbose "Internet connection is ok."
}
function DownloadFile {
CheckInternetConnection
CheckWindowsXPAvaliablitily
Write-Verbose "Started downloading XP Mode executable cabinet from Microsoft server."
$Url = "$BaseURI$($Paths[$Locale])"
$XPModeFile = New-TemporaryFile
Invoke-WebRequest $Url -OutFile $XPModeFile
Rename-Item -Path "$XPModeFile" -NewName "xpmode.exe"
Write-Verbose "Downloading is done."
}
function CheckFileExists {
Write-Verbose "Try to extract XPModeFile (from params) to Temp folder."
if (!(Test-Path -Path $XPModeFile)) {
Write-Error -Message "Cannot find XP Mode file!" -ErrorAction Stop
}
Write-Verbose "Found XP Mode file."
}
function CheckFileSign {
if ((Get-AuthenticodeSignature -FilePath $XPModeFile).Status -ne "Valid") {
Write-Error "XP Mode Executable Cabinet has invalid signature!"
$_ = Read-Host "Continue with invalid signature? ( y / n )"
if ($_ -ne "y") {
Exit-PSSession
} else {
Write-Debug "Continuing with invalid signature..."
}
}
}
function TryUnpackXPModeFile {
CheckFileExists
CheckFileSign
cmd.exe /c "$XPModeFile /X:$TempDirectory\xpmode"
if (!(Test-Path -Path "$TempDirectory\xpmode\sources\xpm")) {
Write-Error -Message "Errors while extracting XP Mode file!" -ErrorAction Stop
}
Write-Verbose "It is avaliable to unpack XP Mode cabinet."
}
function RenameXpmCabinet {
Rename-Item -Path "$TempDirectory\xpmode\sources\xpm" -NewName "xpm.cab"
if (!(Test-Path -Path "$TempDirectory\xpmode\sources\xpm.cab")) {
Write-Error -Message "Errors while renaming 'xpm' file to 'xpm.cab'!" -ErrorAction Stop
}
Write-Verbose "Successfull renaming 'xpm' to 'xpm.cab'."
}
function CheckVMDirectory {
if (!(Test-Path -Path $VMDirectory)) {
Write-Verbose "Did not find VM directory. Creating new directory in user profile..."
New-Item -Path "$env:USERPROFILE" -Name "VirtualMachines" -ItemType "directory"
$VMDirectory = "$env:USERPROFILE\VirtualMachines"
Write-Verbose "Successful creating VM directory."
}
}
function UnpackXpmCabinet {
CheckVMDirectory
RenameXpmCabinet
cmd.exe /c "$env:SystemRoot\system32\expand.exe $TempDirectory\xpmode\sources\xpm.cab -F:VirtualXPVHD $env:USERPROFILE\VirtualMachines"
if (!(Test-Path -Path "$env:USERPROFILE\VirtualMachines\VirtualXPVHD")) {
Write-Error "Unsuccessful virtual machine disk unpacking!" -ErrorAction Stop
}
Write-Verbose "Successful internal cabinet unpacking."
}
function MoveVMToDirectory {
Move-Item -Path "$env:USERPROFILE\VirtualMachines\VirtualXPVHD" -Destination "$env:USERPROFILE\VirtualMachines\VirtualXP.vhd" -Force
Write-Verbose "Successfull extracting."
}
function ExtractXPModeFile {
TryUnpackXPModeFile
UnpackXpmCabinet
MoveVMToDirectory
}
function DownloadOrGetFile {
if (!($XPModeFile) -or !(Test-Path $XPModeFile)) {
DownloadFile
}
ExtractXPModeFile
}
function CreateVM {
New-VM -Name "Windows XP" -MemoryStartupBytes 2048MB -VHDPath "$env:USERPROFILE\VirtualMachines\VirtualXP.vhd"
}
CheckWindowsVersion
CheckWindowsEdition
CheckHyperVEnabled
DownloadOrGetFile
CreateVM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment