Foegi

PS / Clean Win11 install.wim

Feb 20th, 2024
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 13.19 KB | Source Code | 0 0
  1. #Reminder for personal use / Use at your own risk!
  2. #This Script clean Win11 install.wim from Provisioned Apps, set some (German!) Registry Settings, Slipstream Driver and Windows Hoxfixes ect.
  3.  
  4. try {
  5.  
  6. # Set Variables -------------------------------------------------------------------------------------------------------------------------------------
  7.     Write-Host INFO "Set Variables" -ForegroundColor Yellow
  8.     $vPath = 'P:\Windows_WDS_Images\110_Win11\2023-11-11 Win11 Pro 23H2\'
  9.     $mountPath = "$vPath\mount"
  10.     $vUpdatePath = 'P:\Windows_WDS_Images\010_Nlite\01_Updates\11.23H2.x64\'
  11.     $vVMWareTools = 'P:\Windows_WDS_Images\001_Driver\driver_import\VMware-Tools-windows-12.3.5-22544099\'
  12.     $Workstation = $env:COMPUTERNAME
  13.     $ScriptName = '020_Win11_WDS_Manually_Bootimage'
  14.     $Start = Get-Date   # for Timer at end of script
  15.  
  16. # Set Standard Functions  ---------------------------------------------------------------------------------------------------------------------------
  17.     Write-Host INFO "Set Standard Functions" -ForegroundColor Yellow
  18.     Function Push-Error {
  19.         param($exception)
  20.         $line = $exception.InvocationInfo.ScriptLineNumber
  21.         $character = $exception.InvocationInfo.OffsetInLine
  22.         Write-Error "Error and Stop:`nZeile: $line Zeichen $character`n$($exception.Exception.Message)"
  23.     }
  24.  
  25. # Start Prepare BootImage  ----------------------------------------------------------------------------------------------------------------------
  26.     Write-Host "Start Prepare BootImage" -ForegroundColor Yellow
  27.     Write-Host "Set Powershell Location to $vPath" -ForegroundColor Yellow
  28.     Set-Location $vPath
  29.  
  30.     Write-Host "Export Windows 11 Pro / Sourceindex 5 from install.org.wim" -ForegroundColor Yellow
  31.     Dism /Get-ImageInfo /ImageFile:install.org.wim
  32.     Dism /Export-Image /SourceImageFile:install.org.wim /SourceIndex:5 /DestinationImageFile:install.wim  /CheckIntegrity
  33.     Dism /Get-ImageInfo /ImageFile:install.wim
  34.  
  35.     if (-not(Test-Path $vPath'mount')) {
  36.         New-Item -ItemType Directory -Path $vPath'mount' -Force -Verbose
  37.     }
  38.     Write-Host "Mount install.wim into mound folder / $mountPath" -ForegroundColor Yellow
  39.     Dism /Mount-Wim /WimFile:install.wim /Mountdir:"mount" /index:1
  40.  
  41. # Remote AppxPackages ---------------------------------------------------------------------------------------------------------------------------
  42.     Write-Host "List AppxPackages" -ForegroundColor Yellow
  43.     Dism /Image:"mount" /Get-ProvisionedAppxPackages |
  44.     Select-String -Pattern "PackageName:" |
  45.     ForEach-Object {
  46.         "Dism /Image:`"mount`" /Remove-ProvisionedAppxPackage /PackageName:$($_.Line.Split(':')[1].Trim())"
  47.     }
  48.  
  49.     Write-Host "Remove AppxPackages" -ForegroundColor Yellow
  50.     Dism /Image:"mount" /Remove-ProvisionedAppxPackage /PackageName:Microsoft.MicrosoftOfficeHub_18.2204.1141.0_neutral_~_8wekyb3d8bbwe
  51.     Dism /Image:"mount" /Remove-ProvisionedAppxPackage /PackageName:Microsoft.People_2020.901.1724.0_neutral_~_8wekyb3d8bbwe
  52.     Dism /Image:"mount" /Remove-ProvisionedAppxPackage /PackageName:Microsoft.WindowsFeedbackHub_2022.106.2230.0_neutral_~_8wekyb3d8bbwe
  53.     Dism /Image:"mount" /Remove-ProvisionedAppxPackage /PackageName:Microsoft.Xbox.TCUI_1.23.28004.0_neutral_~_8wekyb3d8bbwe
  54.     Dism /Image:"mount" /Remove-ProvisionedAppxPackage /PackageName:Microsoft.XboxGameOverlay_1.47.2385.0_neutral_~_8wekyb3d8bbwe
  55.     Dism /Image:"mount" /Remove-ProvisionedAppxPackage /PackageName:Microsoft.XboxGamingOverlay_2.622.3232.0_neutral_~_8wekyb3d8bbwe
  56.     Dism /Image:"mount" /Remove-ProvisionedAppxPackage /PackageName:Microsoft.XboxIdentityProvider_12.50.6001.0_neutral_~_8wekyb3d8bbwe
  57.     Dism /Image:"mount" /Remove-ProvisionedAppxPackage /PackageName:Microsoft.XboxSpeechToTextOverlay_1.17.29001.0_neutral_~_8wekyb3d8bbwe
  58.     Dism /Image:"mount" /Remove-ProvisionedAppxPackage /PackageName:Microsoft.ZuneMusic_11.2202.46.0_neutral_~_8wekyb3d8bbwe
  59.     Dism /Image:"mount" /Remove-ProvisionedAppxPackage /PackageName:Microsoft.ZuneVideo_2019.22020.10021.0_neutral_~_8wekyb3d8bbwe
  60.  
  61. # Change Registry Settings ContentDeliveryManager / CloudContent --------------------------------------------------------------------------------
  62.     Write-Host "Change Registry Settings ContentDeliveryManager" -ForegroundColor Yellow
  63.     reg load HKU\Default_User "mount\Users\Default\NTUSER.DAT"
  64.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name SystemPaneSuggestionsEnabled -Value 0
  65.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name PreInstalledAppsEnabled -Value 0
  66.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name OemPreInstalledAppsEnabled -Value 0
  67.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name ContentDeliveryAllowed -Value 0
  68.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name FeatureManagementEnabled -Value 0
  69.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name PreInstalledAppsEverEnabled -Value 0
  70.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name SilentInstalledAppsEnabled -Value 0
  71.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name SoftLandingEnabled -Value 0
  72.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name SubscribedContentEnabled -Value 0
  73.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name SubscribedContent-310091Enabled -Value 0
  74.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name SubscribedContent-310092Enabled -Value 0
  75.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name SubscribedContent-338380Enabled -Value 0
  76.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name SubscribedContent-338381Enabled -Value 0
  77.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name SubscribedContent-338387Enabled -Value 0
  78.     Set-ItemProperty -Path Registry::HKU\Default_User\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager -Name SubscribedContent-338388Enabled -Value 0
  79.     Reg Unload HKU\Default_User
  80.  
  81.     Write-Host "Change Registry Settings CloudContent" -ForegroundColor Yellow
  82.     Reg Load HKLM\WIM_SOFTWARE "mount\Windows\System32\config\SOFTWARE"
  83.     New-Item -Path Registry::HKLM\WIM_SOFTWARE\Policies\Microsoft\Windows -Name 'CloudContent' -Force
  84.     New-ItemProperty -PropertyType Dword -Path Registry::HKLM\WIM_SOFTWARE\Policies\Microsoft\Windows\CloudContent -Name 'DisableWindowsConsumerFeatures' -Value '00000001'
  85.     New-ItemProperty -PropertyType Dword -Path Registry::HKLM\WIM_SOFTWARE\Policies\Microsoft\Windows\CloudContent -Name 'DisableSoftLanding' -Value '00000001'
  86.     New-ItemProperty -PropertyType Dword -Path Registry::HKLM\WIM_SOFTWARE\Policies\Microsoft\Windows\CloudContent -Name 'DisableCloudOptimizedContent' -Value '00000001'
  87.     New-ItemProperty -PropertyType Dword -Path Registry::HKLM\WIM_SOFTWARE\Policies\Microsoft\Windows\CloudContent -Name 'DisableConsumerAccountStateContent' -Value '00000001'
  88.     New-ItemProperty -PropertyType Dword -Path Registry::HKLM\WIM_SOFTWARE\Policies\Microsoft\Windows\CloudContent -Name 'DisableWindowsSpotlightFeatures' -Value '00000001'
  89.     New-ItemProperty -PropertyType Dword -Path Registry::HKLM\WIM_SOFTWARE\Policies\Microsoft\Windows\CloudContent -Name 'DisableThirdPartySuggestions' -Value '00000001'
  90.     [GC]::Collect()
  91.     Reg Unload HKLM\WIM_SOFTWARE
  92.  
  93. # Enable .Net Framework 3.5 -------------------------------------------------------------------------------------------------------------------------
  94.     Write-Host "Slipstream NetFx3" -ForegroundColor Yellow
  95.     Dism /Image:$mountPath /Enable-Feature /FeatureName:NetFx3 /All /Source:"sources\sxs\" #/LimitAccess
  96.  
  97. # Change System Settings in Registry ----------------------------------------------------------------------------------------------------------------
  98.     Write-Host "Change System Settings in Registry" -ForegroundColor Yellow
  99.     $systemRegPath = "$mountPath\Windows\System32\config\SYSTEM"
  100.     $loadedHiveKey = "HKLM\TempHive"
  101.     # SYSTEM-Hive laden
  102.     reg load $loadedHiveKey $systemRegPath
  103.     # Disable NtfsDisable8dot3NameCreation
  104.     Set-ItemProperty -Path Registry::$loadedHiveKey\ControlSet001\Control\FileSystem -Name "NtfsDisable8dot3NameCreation" -Value 1 -Verbose
  105.     # NTP-Servereinstellungen in der Registry konfigurieren
  106.     New-Item -Path "Registry::$loadedHiveKey\Software\Policies\Microsoft\W32Time\Parameters" -Force -Verbose
  107.     Set-ItemProperty -Path "Registry::$loadedHiveKey\Software\Policies\Microsoft\W32Time\Parameters" -Name "NtpServer" -Value "0.at.pool.ntp.org,1.at.pool.ntp.org,2.at.pool.ntp.org,3.at.pool.ntp.org" -Verbose
  108.     # Netzwerkerkennungseinstellungen in der Registry konfigurieren
  109.     New-Item -Path "Registry::$loadedHiveKey\Software\Microsoft\Windows\CurrentVersion\NcdAutoSetup" -Name 'Private' -Force -Verbose
  110.     New-ItemProperty -Type Dword -Path "Registry::$loadedHiveKey\Software\Microsoft\Windows\CurrentVersion\NcdAutoSetup\Private" -Name 'AutoSetup' -Value 0 -Verbose
  111.     # Remove Teams
  112.     New-Item -Path "Registry::$loadedHiveKey\SOFTWARE\Microsoft\Windows\CurrentVersion\Communications" -Name 'ConfigureChatAutoInstall' -Force -Verbose
  113.     New-ItemProperty -Path "Registry::$loadedHiveKey\SOFTWARE\Microsoft\Windows\CurrentVersion\Communications" -Name 'ConfigureChatAutoInstall' -Value 0 -Type DWord -Force -Verbose
  114.     [GC]::Collect()
  115.     Reg Unload $loadedHiveKey
  116.  
  117. # Change Default User Settings in Registry ----------------------------------------------------------------------------------------------------------------
  118.     # Default User NTUSER.DAT laden und ändern
  119.     $defaultUserRegPath = "$mountPath\Users\Default\NTUSER.DAT"
  120.     $loadedHiveKey = "HKLM\DefaultUserHive"
  121.     Write-Host "Change Default User Settings in Registry" -ForegroundColor Yellow
  122.     reg load $loadedHiveKey $defaultUserRegPath
  123.     #Set thousand dot .
  124.     Set-ItemProperty -Path "Registry::$loadedHiveKey\Control Panel\International" -Name sThousand -Value '.' -Verbose
  125.     # Setze das standardmäßige Tastaturlayout
  126.     Set-ItemProperty -Path "Registry::$loadedHiveKey\Keyboard Layout\Preload" -Name 1 -Value '00000c07' -Verbose
  127.     # Setze Tastaturlayout-Substitut
  128.     Set-ItemProperty -Path "Registry::$loadedHiveKey\Keyboard Layout\Substitutes" -Name '00000c07' -Value '00010407' -Verbose
  129.     # Setze LegacyDefaultPrinterMode
  130.     Set-ItemProperty -Path "Registry::$loadedHiveKey\Software\Microsoft\Windows NT\CurrentVersion\Windows" -Name "LegacyDefaultPrinterMode" -Value 1 -Verbose
  131.     # Remove OneDriveSetup
  132.     Remove-ItemProperty -Path Registry::$loadedHiveKey\Software\Microsoft\Windows\CurrentVersion\Run -Name 'OneDriveSetup' -Verbose
  133.     [GC]::Collect()
  134.     Reg Unload $loadedHiveKey
  135.  
  136. # Remove OneDrive Setup in File and Registry --------------------------------------------------------------------------------------------------------
  137.     Write-Host "Remove OneDrive Setup in File and Registry" -ForegroundColor Yellow
  138.     Remove-Item -Path $mountPath'\Users\Default\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\OneDrive.lnk' -Force -Verbose -ErrorAction SilentlyContinue
  139.     Remove-Item -Path $mountPath'\Windows\System32\OneDriveSetup.exe' -Force -Verbose -ErrorAction SilentlyContinue
  140.     Remove-Item -Path $mountPath'\Windows\SysWOW64\OneDriveSetup.exe' -Force -Verbose -ErrorAction SilentlyContinue
  141.  
  142. # Slipstream Updates / VMware Driver ----------------------------------------------------------------------------------------------------------------
  143.     Write-Host "Slipstream Updates" -ForegroundColor Yellow
  144.     Dism /image:"mount" /Add-Package /Packagepath:$vUpdatePath
  145.     Write-Host "Slipstream VMware Driver" -ForegroundColor Yellow
  146.     Dism /Image:"mount" /Add-Driver /Driver:$vVMWareTools /recurse
  147.  
  148. # Commit Change -------------------------------------------------------------------------------------------------------------------------------------
  149.     Write-Host "Commit Change in install.wim" -ForegroundColor Yellow
  150.     Dism /unmount-wim /Mountdir:"mount" /commit
  151.     #Dism /unmount-wim /Mountdir:"mount" /discard
  152.     Write-Host "Cleanut wim mount" -ForegroundColor Yellow
  153.     DISM /Cleanup-Wim
  154. }
  155.  
  156. catch {
  157.         Push-Error $_
  158.     }
  159. finally {
  160.     $End = Get-Date
  161.     $TimeSpan = New-TimeSpan -Start $Start -End $End
  162.     $TimeSpanText = "[$Workstation] $ScriptName done in: {0:hh} hours {0:mm} minutes and {0:ss} seconds." -f $TimeSpan
  163.     Write-Host "$TimeSpanText" -ForegroundColor Yellow
  164.  
  165.     try { stop-transcript } catch { $error.clear() }
  166.     [GC]::Collect()
  167.     Pause
  168.     #Exit
  169. }
Advertisement
Add Comment
Please, Sign In to add comment