Skip to content

Instantly share code, notes, and snippets.

@rupertbenbrook
Last active May 7, 2017 15:23
Show Gist options
  • Save rupertbenbrook/ae369cf962b7b3e134aa027d83bff4e3 to your computer and use it in GitHub Desktop.
Save rupertbenbrook/ae369cf962b7b3e134aa027d83bff4e3 to your computer and use it in GitHub Desktop.
Basic initial set up of a Windows VM
#
# To run this script from here, run the following in an admin PowerShell console:
# . { iwr -useb https://gist.githubusercontent.com/rupertbenbrook/ae369cf962b7b3e134aa027d83bff4e3/raw/DownloadUpdates.ps1 } | iex
#
# Download and install any updates, rebooting if needed
Write-Host "Searching for updates..."
$update = New-Object -ComObject "Microsoft.Update.Session"
$updateSearch = $update.CreateUpdateSearcher()
$result = $updateSearch.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
if ($result.Updates.Count -gt 0) {
$result.Updates | Format-Table Title
$downloads = New-Object -ComObject "Microsoft.Update.UpdateColl"
$result.Updates | %{ $null = $downloads.Add($_) }
$downloader = $update.CreateUpdateDownloader()
$downloader.Updates = $downloads
Write-Host "Downloading updates..."
$null = $downloader.Download()
$install = New-Object -ComObject "Microsoft.Update.UpdateColl"
$result.Updates | ? IsDownloaded -eq $true | %{ $null = $install.Add($_) }
$installer = $update.CreateUpdateInstaller()
$installer.Updates = $install
Write-Host "Installing updates..."
while ($installer.IsBusy) {
Write-Host " (Windows Update is busy...waiting)"
Start-Sleep -Seconds 10
}
$installResult = $installer.Install()
if ($installResult.RebootRequired -eq $true) {
Write-Host "*** REBOOT REQUIRED ***"
}
}
#
# To run this script from here, run the following in an admin PowerShell console:
# . { iwr -useb https://gist.githubusercontent.com/rupertbenbrook/ae369cf962b7b3e134aa027d83bff4e3/raw/SetUpVm.ps1 } | iex
#
# Set network profile to private
Get-NetConnectionProfile | Set-NetConnectionProfile -NetworkCategory Private
# Allow file sharing (and ping)
Set-NetFirewallRule -DisplayGroup "File And Printer Sharing" -Enabled True
# Allow network discovery
Set-NetFirewallRule -DisplayGroup "Network Discovery" -Enabled True
# Start Server Manager performance counters
Enable-ScheduledTask -TaskName "Server Manager Performance Monitor" -TaskPath "\Microsoft\Windows\PLA\"
Start-ScheduledTask -TaskName "Server Manager Performance Monitor" -TaskPath "\Microsoft\Windows\PLA\"
# Update and download PowerShell help
Update-Help -Force -ErrorAction SilentlyContinue
# Remove SMB v1 support
Uninstall-WindowsFeature -Name FS-SMB1
# Turn off IE enhanced security for admins and users
$adminKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}"
$userKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}"
Set-ItemProperty -Path $adminKey -Name "IsInstalled" -Value 0 -Force
Set-ItemProperty -Path $userKey -Name "IsInstalled" -Value 0 -Force
Stop-Process -Name Explorer -ErrorAction SilentlyContinue
# Enable Windows SmartScreen
$smartScreenKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer"
Set-ItemProperty -Path $smartScreenKey -Name "SmartScreenEnabled" -Value "RequireAdmin" -Force
# Turn off IE protected mode prompt
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Internet Explorer\Main" -Name NoProtectedModeBanner -Value 1
# Set current user region, language, location and keyboard layout to UK
Set-Culture -CultureInfo en-GB
Set-WinSystemLocale -SystemLocale en-GB
Set-WinHomeLocation -GeoId 242
Set-WinUserLanguageList -LanguageList en-GB -Force
# Load new user registry hive
reg load "HKU\Temp" "$env:SystemDrive\Users\Default\NTUSER.DAT"
# Set current user region, language, location and keyboard layout to default user, new user, system, local service and network service
".DEFAULT","Temp","S-1-5-18","S-1-5-19","S-1-5-20" | %{
$User = $_
"Control Panel\Input Method\","Control Panel\International\","Keyboard Layout\" | %{
@{Source="HKCU:\$_";Destination="Microsoft.PowerShell.Core\Registry::HKEY_USERS\$User\$_" }
} | %{
"$User -> $($_.Source)"
$null = New-Item -Path $_.Destination -Force
$null = Copy-Item -Path $_.Source -Destination (Split-Path -Path $_.Destination -Parent) -Recurse -Force
}
}
# Unload new user registry hive
Start-Sleep -Seconds 5
reg unload "HKU\Temp"
# Enable Microsoft Update
$serviceManager = New-Object -ComObject "Microsoft.Update.ServiceManager"
$serviceManager.ClientApplicationID = "My App"
$serviceManager.AddService2("7971f918-a847-4430-9279-4a52d1efe18d", 7, "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment