-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pure11.ps1
1149 lines (1010 loc) · 49.9 KB
/
Pure11.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#requires -runasadministrator
<#
.SYNOPSIS
Pure11. Windows 11, minus the junk they thought you needed.
.DESCRIPTION
Windows 11 arrives loaded with more than just an operating system—there's also bloatware, relentless telemetry, and a host of so-called 'features' designed to serve everyone except the end user.
Pure11 cuts through the nonsense, providing a streamlined, privacy-focused, and highly efficient version of the operating system.
This PowerShell script strips away the bloatware, removes ads, disables intrusive features, and performs essential system hardening, delivering a clean, secure, and responsive Windows 11 installation
Pure11 is for those who prefer an OS that works for them, not the other way around.
.PARAMETER BuildIso
Builds the Pure 11 install iso.
.PARAMETER Init
It creates the required folder structure and exit.
.LINK
https://github.com/Orfeous/Pure11
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false, HelpMessage = "Initializes the folder structure.")]
[switch] $Init,
[Parameter(Mandatory = $false, HelpMessage = "Builds a Pure11 install iso.")]
[switch] $BuildIso
);
#region Imports
Add-Type -AssemblyName System.Windows.Forms; # Required to display gui stuff.
Add-Type -AssemblyName System.Drawing; # Same.
#endregion
#region Script State
[hashtable] $P11State = @{}; # Shared state for the script.
[array] $P11State.DetectedWindowsImages = @(); # List of the SKUs detected in install.wim (IsoBuild).
[string] $P11State.ImageName = [string]::Empty; # Name of the selected SKU, substitute with a string if you want to make the script automatic (IsoBuild). Ex: "Windows 11 Enterprise"
[string] $P11State.Language = [string]::Empty; # Primary language of the image (detected)
[string] $P11State.Edition = [string]::Empty; # Edition of the selected image (detected)
[string] $P11State.Architecture = [string]::Empty; # Architecture of the selected image (detected)
[string] $P11State.Build = [string]::Empty; # Build.SPBuild of the selected image (detected)
[string] $P11State.IsoPath = [string]::Empty; # Full path to the source ISO
[string] $P11State.IsoDrive = [string]::Empty; # Drive letter of the mounted source iso
#endregion
#region Constants & Globals
# Psst!
$ProgressPreference = 'SilentlyContinue';
# Consistent file-path friendly timestamp.
$P11DateTime = [DateTime]::Now.ToString("yyyy-MM-dd_HH-mm");
# Static locations
[hashtable] $P11Paths = @{};
[string] $P11Paths.LogDirectory = "${PSScriptRoot}\logs"; # Logs are stored here.
[string] $P11Paths.ConfigDirectory = "${PSScriptRoot}\config"; # Pure11 script config files are stored here.
[string] $P11Paths.RegistryConfigDirectory = "${PSScriptRoot}\config\reg"; # Registry customization files.
[string] $P11Paths.PostInstallScriptsDirectory = "${PSScriptRoot}\config\post-install"; # Scripts to be run on the deployed image.
[string] $P11Paths.IsoDirectory = "${PSScriptRoot}\iso"; # Generated ISO files are stored here.
[string] $P11Paths.WorkDirectory = "${PSScriptRoot}\work"; # The script will work inside this directory.
[string] $P11Paths.IsoSourceDirectory = "$($P11Paths.WorkDirectory)\iso-source"; # The contents of the source iso are cached here.
[string] $P11Paths.MountDirectory = "$($P11Paths.WorkDirectory)\mount"; # The wim files will be mounted here.
[string] $P11Paths.ScratchDirectory = "$($P11Paths.WorkDirectory)\scratch"; # Used by dism operations.
[string] $P11Paths.DriversDirectory = "$($P11Paths.WorkDirectory)\drivers"; # Place drivers here, they will be added to the image.
[string] $P11Paths.UpdatesDirectory = "$($P11Paths.WorkDirectory)\updates"; # Place windows updates here, they will be added to the image.
[string] $P11Paths.ScriptLogFile = "$($P11Paths.LogDirectory)\${P11DateTime}.Pure11.log"; # Pure11 script log.
[string] $P11Paths.DismLogFile = "$($P11Paths.LogDirectory)\${P11DateTime}.Dism.log"; # Dism log.
[string] $P11Paths.InstallWim = "$($P11Paths.IsoSourceDirectory)\sources\install.wim"; # Main install wim containing the deployable image.
[string] $P11Paths.BootWim = "$($P11Paths.IsoSourceDirectory)\sources\boot.wim"; # Boot and windows setup image.
[string] $P11Paths.OscdimgExe = "$($P11Paths.WorkDirectory)\oscdimg.exe"; # Used to create the bootable iso.
[string] $P11Paths.OscdimgUrl = "https://msdl.microsoft.com/download/symbols/oscdimg.exe/3D44737265000/oscdimg.exe"; # MS Symbol server to download oscdimg.exe from official source.
[string] $P11Paths.OscdimgChecksum = "F5129F313ED7EB46F2677CF522E64264A225F226307ED0DDB52BB14C46E7CFDD"; # SHA256 checksum of oscdimg.exe
[string] $P11Paths.AutounattendXml = "$($P11Paths.ConfigDirectory)\autounattend.xml"; # Source unattended file.
[string] $P11Paths.AutounattendXmlIso = "$($P11Paths.IsoSourceDirectory)\autounattend.xml"; # Destination unattended file.
[string] $P11Paths.AppxConfig = "$($P11Paths.ConfigDirectory)\appx.conf"; # Config file for AppXPackages to be removed.
[string] $P11Paths.FeaturesConfig = "$($P11Paths.ConfigDirectory)\features.conf"; # Config file for OptionalFeatures to be removed.
[string] $P11Paths.CapabilitiesConfig = "$($P11Paths.ConfigDirectory)\capabilities.conf"; # Config file for Capabilities to be removed.
[string] $P11Paths.FilesConfig = "$($P11Paths.ConfigDirectory)\files.txt"; # Config file for Files and Directories to be removed. Relative to [MountDirectory].
[string] $P11Paths.HostsConfig = "$($P11Paths.ConfigDirectory)\hosts.conf"; # Config file for DNS entries to be blocked by hosts file.
[string] $P11Paths.SysReqRegConfig = "$($P11Paths.RegistryConfigDirectory)\system-requirements.reg"; # System Requirement bypass registry entries.
# [string] $P11Paths. # Template for copy & paste <3
[hashtable] $P11Config = @{}; # Information loaded from the config files.
[array] $P11Config.AppxPackagesToRemove = @(); # List of AppXPackages to be removed.
[array] $P11Config.FeaturesToRemove = @(); # List of OptionalFeatures to be removed.
[array] $P11Config.CapabilitiesToRemove = @(); # List of Capabilities to be removed.
[array] $P11Config.PathsToRemove = @(); # List of Files and Directories to be removed. (Already prefixed with [MountDirectory])
[array] $P11Config.HostsToBlock = @(); # List of DNS entries to be blocked by hosts file.
[array] $P11Config.RegistryFiles = @(); # List of reg files. (Minus the one defined in [SysReqRegConfig])
[array] $P11Config.PostInstallScripts = @(); # List of scripts to be added to the image and setup to be executed when the user logs in for the first time.
#endregion
#region Helper Functions
function Write-Log {
<#
.SYNOPSIS
Logging function.
.PARAMETER Message
Log message.
.PARAMETER Function
Parent funtion.
.PARAMETER Severity
Severity of the message.
#>
[CmdletBinding()] [OutputType([System.Void])]
param(
[Parameter(Mandatory = $true, HelpMessage = "Log message.")]
[ValidateNotNullOrEmpty()]
[string] $Message,
[Parameter(Mandatory = $false, HelpMessage = "Parent funtion.")]
[ValidateNotNullOrEmpty()]
[string] $Function,
[Parameter(Mandatory = $false, HelpMessage = "Severity of the message.")]
[ValidateNotNullOrEmpty()]
[ValidateSet('Debug', 'Info', 'Warning', 'Error', 'General')]
[string] $Severity = 'General'
);
switch ($Severity) {
"Debug" {
[System.ConsoleColor] $messageColor = 'DarkMagenta';
};
"Info" {
[System.ConsoleColor] $messageColor = 'DarkGreen';
};
"Warning" {
[System.ConsoleColor] $messageColor = 'DarkYellow';
};
"Error" {
[System.ConsoleColor] $messageColor = 'DarkRed';
};
Default {
[System.ConsoleColor] $messageColor = 'DarkGray';
};
};
$logTime = [DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss");
$logSeverity = $Severity.ToUpper();
$logMessage = "[${logTime}]";
$logMessage += "[${logSeverity}]";
if (-not([string]::IsNullOrEmpty($Function))) { $logMessage += "[${Function}]"; };
$logMessage += "${Message}";
# Write-Host "[${logTime}][${logSeverity}]${Message}" -ForegroundColor $messageColor;
Write-Host "${logMessage}" -ForegroundColor $messageColor;
};
function New-P11Directories {
<#
.SYNOPSIS
Creates the directory layout needed for the script to work.
#>
[CmdletBinding()] [OutputType([System.Void])] param();
New-Item -Path "$($P11Paths.LogDirectory)" -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null;
New-Item -Path "$($P11Paths.ConfigDirectory)" -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null;
New-Item -Path "$($P11Paths.RegistryConfigDirectory)" -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null;
New-Item -Path "$($P11Paths.IsoDirectory)" -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null;
New-Item -Path "$($P11Paths.WorkDirectory)" -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null;
New-Item -Path "$($P11Paths.IsoSourceDirectory)" -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null;
New-Item -Path "$($P11Paths.MountDirectory)" -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null;
New-Item -Path "$($P11Paths.ScratchDirectory)" -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null;
New-Item -Path "$($P11Paths.UpdatesDirectory)" -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null;
New-Item -Path "$($P11Paths.DriversDirectory)" -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null;
Write-Log -Message "Pure11 directory structure created." -Severity Info;
};
function Test-P11RunAsSystem {
<#
.SYNOPSIS
Validates if the script is running as NT AUTHORITY\SYSTEM.
#>
[CmdletBinding()] [OutputType([System.Void])] param();
if ($(whoami) -ne "nt authority\system") {
$errorMessage = "This script is designed to be executed as 'NT AUTHORITY\SYSTEM'!`n`a";
$errorMessage += "You can do so by opening powershell through PsExec.`n";
$errorMessage += "`tpsexec64.exe -nobanner -s -i powershell`n";
$errorMessage += "and then running Pure11.ps1`n";
Write-Log -Message "${errorMessage}" -Severity Error;
throw "Permission error!";
};
};
function Get-P11OscdimgExe {
<#
.SYNOPSIS
Downloads oscdimg.exe from microsoft.
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Invoke-WebRequest -Uri $P11Paths.OscdimgUrl -OutFile "$($P11Paths.OscdimgExe)";
if ((Get-FileHash -Path "$($P11Paths.OscdimgExe)" -Algorithm SHA256).Hash -ne $P11Paths.OscdimgChecksum) {
throw "oscdimg.exe is corrupt!";
};
Write-Log -Message "oscdimg.exe downloaded." -Severity Info;
};
function Test-P11OscdimgExe {
<#
.SYNOPSIS
Validates the availability of oscdimg.exe.
#>
[CmdletBinding()] [OutputType([System.Void])] param();
if (Test-Path -Path "$($P11Paths.OscdimgExe)") {
Write-Log -Message "Local oscdimg.exe found: $($P11Paths.OscdimgExe)" -Severity Debug;
if ((Get-FileHash -Path "$($P11Paths.OscdimgExe)" -Algorithm SHA256).Hash -ne $P11Paths.OscdimgChecksum) {
throw "oscdimg.exe is corrupt!";
};
Write-Log -Message "Local oscdimg.exe hash matches! Yaay \o/" -Severity Debug;
}
else {
Get-P11OscdimgExe;
};
};
function Test-P11IsoSource {
<#
.SYNOPSIS
Simple check to make sure the source is what it should be (:
#>
[CmdletBinding()] [OutputType([System.Void])] param();
if (-not(Test-Path -Path "$($P11Paths.InstallWim)")) { throw "Install.wim not found in $($P11Paths.IsoDirectory)"; };
if (-not(Test-Path -Path "$($P11Paths.BootWim)")) { throw "Boot.wim not found in $($P11Paths.IsoDirectory)"; };
};
function Get-P11Config {
<#
.SYNOPSIS
Helper function to load the config file contents. Not antipattern at all... ¯\_(ツ)_/¯
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Loading config..." -Severity Info;
# Load AppX
Get-Content -Path "$($P11Paths.AppxConfig)" | Where-Object { (-not $_.StartsWith('#')) -and (-not ([string]::IsNullOrEmpty($_))) } | ForEach-Object {
$Script:P11Config.AppxPackagesToRemove += $_;
};
Write-Log -Message "Loaded $($P11Config.AppxPackagesToRemove.Count) Appx packages to remove." -Severity Debug;
# Load Features
Get-Content -Path "$($P11Paths.FeaturesConfig)" | Where-Object { (-not $_.StartsWith('#')) -and (-not ([string]::IsNullOrEmpty($_))) } | ForEach-Object {
$Script:P11Config.FeaturesToRemove += $_;
};
Write-Log -Message "Loaded $($P11Config.FeaturesToRemove.Count) Features to remove." -Severity Debug;
# Load Capabilities
Get-Content -Path "$($P11Paths.CapabilitiesConfig)" | Where-Object { (-not $_.StartsWith('#')) -and (-not ([string]::IsNullOrEmpty($_))) } | ForEach-Object {
$Script:P11Config.CapabilitiesToRemove += $_;
};
Write-Log -Message "Loaded $($P11Config.CapabilitiesToRemove.Count) Capabilities to remove." -Severity Debug;
# Load Hosts
Get-Content -Path "$($P11Paths.HostsConfig)" | Where-Object { (-not $_.StartsWith('#')) -and (-not ([string]::IsNullOrEmpty($_))) } | ForEach-Object {
$Script:P11Config.HostsToBlock += $_;
};
Write-Log -Message "Loaded $($P11Config.HostsToBlock.Count) Hosts to block." -Severity Debug;
# Load File & Folder paths
Get-Content -Path "$($P11Paths.FilesConfig)" | Where-Object { (-not $_.StartsWith('#')) -and (-not ([string]::IsNullOrEmpty($_))) } | ForEach-Object {
$Script:P11Config.PathsToRemove += "$($P11Paths.MountDirectory)\$($_)";
};
Write-Log -Message "Loaded $($P11Config.PathsToRemove.Count) filesystem paths to remove." -Severity Debug;
# Load registry tweak files
# Exclude the one which sets the system requirements.
# Exclude the ones which uses a dash (-) as the first character in their name.
Get-ChildItem -Path "$($P11Paths.RegistryConfigDirectory)" -Include "*.reg" -Recurse | Where-Object { ($_.FullName -ne "$($P11Paths.SysReqRegConfig)") -and (-not($_.Name.StartsWith('-'))) } | ForEach-Object {
$Script:P11Config.RegistryFiles += "$($_.FullName)";
};
Write-Log -Message "Loaded $($P11Config.RegistryFiles.Count) registry files." -Severity Debug;
# Load post install scripts
Get-ChildItem -Path "$($P11Paths.PostInstallScriptsDirectory)" | Select-Object -Property Name, FullName | ForEach-Object {
$P11Config.PostInstallScripts += $_;
};
Write-Log -Message "Loaded $($P11Config.PostInstallScripts.Count) post-install script(s)." -Severity Debug;
};
function Get-P11DismBaseArgs {
<#
.SYNOPSIS
It generates the base splat object for dism stuff.
#>
[CmdletBinding()] [OutputType([hashtable])]
param(
[Parameter(Mandatory = $false, HelpMessage = "Adds the mount directory path.")]
[switch] $WithPath,
[Parameter(Mandatory = $false, HelpMessage = "Adds the install.wim image path.")]
[switch] $InstallWim,
[Parameter(Mandatory = $false, HelpMessage = "Adds the boot.wim image path.")]
[switch] $BootWim
);
[hashtable] $dismArgs = @{
ScratchDirectory = "$($P11Paths.ScratchDirectory)";
LogPath = "$($P11Paths.DismLogFile)";
LogLevel = "Warnings";
};
if ($WithPath.IsPresent) {
$dismArgs.Path = "$($P11Paths.MountDirectory)";
};
if ($InstallWim.IsPresent) {
$dismArgs.ImagePath = "$($P11Paths.InstallWim)";
}
elseif ($BootWim.IsPresent) {
$dismArgs.ImagePath = "$($P11Paths.BootWim)";
};
return $dismArgs;
};
function Mount-P11Wim {
<#
.SYNOPSIS
Helper function to mount wim files.
#>
[CmdletBinding()] [OutputType([System.Void])]
param(
[Parameter(Mandatory = $false, HelpMessage = "Mounts install.wim.")]
[switch] $InstallWim,
[Parameter(Mandatory = $false, HelpMessage = "Mounts boot.wim")]
[switch] $BootWim
);
if ($InstallWim.IsPresent) {
$MountWindowsImageArgs = Get-P11DismBaseArgs -InstallWim -WithPath;
$MountWindowsImageArgs.Index = 1; # install.wim should only have 1 index left after (Remove-P11UnusedSku).
$image = "install.wim"; # for logging
}
elseif ($BootWim.IsPresent) {
$MountWindowsImageArgs = Get-P11DismBaseArgs -BootWim -WithPath;
$MountWindowsImageArgs.Index = 2; # Index=2 is Windows Setup.
$image = "boot.wim"; # for logging
};
Write-Log -Message "Mounting ${image}..." -Severity Info;
Mount-WindowsImage @MountWindowsImageArgs | Out-Null;
};
function Dismount-P11Wim {
<#
.SYNOPSIS
Helper function to dismount wim files.
#>
[CmdletBinding()] [OutputType([System.Void])]
param(
[Parameter(Mandatory = $false, HelpMessage = "Dismounts install.wim.")]
[switch] $InstallWim
);
$image = "boot.wim"; # for logging
$DismountWindowsImageArgs = Get-P11DismBaseArgs -WithPath;
$DismountWindowsImageArgs.Save = $true;
$DismountWindowsImageArgs.CheckIntegrity = $true;
if ($InstallWim.IsPresent) {
$image = "install.wim"; # for logging
$RepairWindowsImageArgs = Get-P11DismBaseArgs -WithPath;
$RepairWindowsImageArgs.LimitAccess = $true;
$RepairWindowsImageArgs.StartComponentCleanup = $true;
$RepairWindowsImageArgs.ResetBase = $true;
Write-Log -Message "Clearing ${image}..." -Severity Info;
Repair-WindowsImage @RepairWindowsImageArgs | Out-Null;
};
Write-Log -Message "Dismounting ${image}..." -Severity Info;
Dismount-WindowsImage @DismountWindowsImageArgs | Out-Null;
};
function Mount-P11RegistryHive {
<#
.SYNOPSIS
Mounts registry hives.
#>
[CmdletBinding()] [OutputType([System.Void])]
param(
[Parameter(Mandatory = $false, HelpMessage = "Mounts all registry hives.")]
[switch] $All,
[Parameter(Mandatory = $false, HelpMessage = "Mounts the COMPONENTS registry hive.")]
[switch] $Components,
[Parameter(Mandatory = $false, HelpMessage = "Mounts the DEFAULT registry hive.")]
[switch] $Default,
[Parameter(Mandatory = $false, HelpMessage = "Mounts the NTUSER registry hive.")]
[switch] $NTUSER,
[Parameter(Mandatory = $false, HelpMessage = "Mounts the SOFTWARE registry hive.")]
[switch] $Software,
[Parameter(Mandatory = $false, HelpMessage = "Mounts the SYSTEM registry hive.")]
[switch] $System
);
if ($All.IsPresent) {
Write-Log -Message "Mounting all registry hives..." -Severity Debug;
$(& "reg" "load" "HKLM\P11_COMPONENTS" "$($P11Paths.MountDirectory)\Windows\System32\config\COMPONENTS" 2>&1) | Out-Null;
$(& "reg" "load" "HKLM\P11_DEFAULT" "$($P11Paths.MountDirectory)\Windows\System32\config\default" 2>&1) | Out-Null;
$(& "reg" "load" "HKLM\P11_NTUSER" "$($P11Paths.MountDirectory)\Users\Default\ntuser.dat" 2>&1) | Out-Null;
$(& "reg" "load" "HKLM\P11_SOFTWARE" "$($P11Paths.MountDirectory)\Windows\System32\config\SOFTWARE" 2>&1) | Out-Null;
$(& "reg" "load" "HKLM\P11_SYSTEM" "$($P11Paths.MountDirectory)\Windows\System32\config\SYSTEM" 2>&1) | Out-Null;
}
else {
if ($Components.IsPresent) {
Write-Log -Message "Mounting the COMPONENTS registry hive." -Severity Debug;
$(& "reg" "load" "HKLM\P11_COMPONENTS" "$($P11Paths.MountDirectory)\Windows\System32\config\COMPONENTS" 2>&1) | Out-Null;
};
if ($Default.IsPresent) {
Write-Log -Message "Mounting the DEFAULT registry hive." -Severity Debug;
$(& "reg" "load" "HKLM\P11_DEFAULT" "$($P11Paths.MountDirectory)\Windows\System32\config\default" 2>&1) | Out-Null;
};
if ($NTUSER.IsPresent) {
Write-Log -Message "Mounting the NTUSER registry hive." -Severity Debug;
$(& "reg" "load" "HKLM\P11_NTUSER" "$($P11Paths.MountDirectory)\Users\Default\ntuser.dat" 2>&1) | Out-Null;
};
if ($Software.IsPresent) {
Write-Log -Message "Mounting the SOFTWARE registry hive." -Severity Debug;
$(& "reg" "load" "HKLM\P11_SOFTWARE" "$($P11Paths.MountDirectory)\Windows\System32\config\SOFTWARE" 2>&1) | Out-Null;
};
if ($System.IsPresent) {
Write-Log -Message "Mounting the SYSTEM registry hive." -Severity Debug;
$(& "reg" "load" "HKLM\P11_SYSTEM" "$($P11Paths.MountDirectory)\Windows\System32\config\SYSTEM" 2>&1) | Out-Null;
};
};
};
function Dismount-P11RegistryHives {
<#
.SYNOPSIS
Dismounts registry hives.
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Dismounting registry hives." -Severity Debug;
$(& "reg" "unload" "HKLM\P11_COMPONENTS" 2>&1) | Out-Null;
$(& "reg" "unload" "HKLM\P11_DEFAULT" 2>&1) | Out-Null;
$(& "reg" "unload" "HKLM\P11_SOFTWARE" 2>&1) | Out-Null;
$(& "reg" "unload" "HKLM\P11_SYSTEM" 2>&1) | Out-Null;
$(& "reg" "unload" "HKLM\P11_NTUSER" 2>&1) | Out-Null;
};
function Add-P11HostsFileBlockEntry {
<#
.SYNOPSIS
Creates entries in the hosts file to block the provided domain. Covers both ipv4 and ipv6.
.LINK
Proudly stolen from:
https://privacy.sexy/
https://github.com/undergroundwires/privacy.sexy
#>
[CmdletBinding()] [OutputType([System.Void])]
param(
[Parameter(Mandatory = $true, HelpMessage = "DNS entry to be blocked.")]
[string] $Domain
);
$hostsFilePath = "$($P11Paths.MountDirectory)\Windows\System32\drivers\etc\hosts";
$comment = "Managed by Pure11";
# $hostsFileEncoding = [Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]::Utf8;
# $hostsFileEncoding = [System.Text.Encoding]::UTF8;
$hostsFileEncoding = 'UTF8'; # idk which one hurts more, time zones or character encoding?!
$blockingHostsEntries = @(;
@{ AddressType = "IPv4";
IPAddress = '0.0.0.0';
};
@{ AddressType = "IPv6";
IPAddress = '::1';
};
);
try {
$isHostsFilePresent = Test-Path -Path "${hostsFilePath}" -PathType Leaf -ErrorAction Stop;
}
catch {
Write-Log -Message "Failed to check hosts file existence. Error: $($_)`n$($_.ScriptStackTrace)" -Severity Error;
return;
};
if (-not $isHostsFilePresent) {
Write-Log -Message "Creating a new hosts file at ${hostsFilePath}." -Severity Debug;
try {
New-Item -Path $hostsFilePath -ItemType File -Force -ErrorAction Stop | Out-Null;
Write-Host "Successfully created the hosts file." -ForegroundColor DarkBlue;
}
catch {
Write-Log -Message "Failed to create the hosts file. Error: $($_)`n$($_.ScriptStackTrace)" -Severity Error;
return;
};
};
Write-Log -Message "Processing addition for '${Domain}' entry." -Severity Debug;
foreach ($blockingEntry in $blockingHostsEntries) {
try {
$hostsFileContents = Get-Content -Path "${hostsFilePath}" -Raw -Encoding $hostsFileEncoding -ErrorAction Stop;
}
catch {
Write-Log -Message "Failed to read the hosts file. Error: $($_)`n$($_.ScriptStackTrace)" -Severity Error;
continue;
};
$hostsEntryLine = "$($blockingEntry.IPAddress)`t${Domain} $([char]35) ${comment}";
if ((-not [String]::IsNullOrWhiteSpace($hostsFileContents)) -and ($hostsFileContents.Contains($hostsEntryLine))) {
continue;
};
try {
Add-Content -Path "${hostsFilePath}" -Value "${hostsEntryLine}" -Encoding $hostsFileEncoding -ErrorAction Stop;
}
catch {
Write-Log -Message "Failed to add the entry. Error: $($_)`n$($_.ScriptStackTrace)" -Severity Error;
continue;
};
}
};
function Get-P11WindowsImageInfo {
<#
.SYNOPSIS
Retrieves image information from install.wim and stores it in [P11State].
#>
[CmdletBinding()] [OutputType([System.Void])] param();
$ArchString = @{ [UInt32]0 = 'x86'; [UInt32]5 = 'arm'; [UInt32]6 = 'ia64'; [UInt32]9 = 'amd64'; [UInt32]12 = 'arm64' };
$GetWindowsImageArgs = Get-P11DismBaseArgs -InstallWim;
$GetWindowsImageArgs.Index = 1;
$imageInfo = (Get-WindowsImage @GetWindowsImageArgs | Select-Object -Property *);
$Script:P11State.Language = ($imageInfo.Languages[0]);
$Script:P11State.Edition = $imageInfo.EditionId;
$Script:P11State.Architecture = $ArchString[$imageInfo.Architecture];
$Script:P11State.Build = "$($imageInfo.Build).$($imageInfo.SPBuild)";
Write-Log -Message "Selected Image is: $($P11State.ImageName)" -Severity Info;
Write-Log -Message "Selected Image Language: $($P11State.Language)" -Severity Info;
Write-Log -Message "Selected Image Edition: $($P11State.Edition)" -Severity Info;
Write-Log -Message "Selected Image Architecture: $($P11State.Architecture)" -Severity Info;
Write-Log -Message "Selected Image Build: $($P11State.Build)" -Severity Info;
};
function Copy-P11GuiRecursiveWithProgress {
<#
.SYNOPSIS
Gui (Windows Shell) progress window for file copy operations.
#>
[CmdletBinding()] [OutputType([System.Void])]
param(
[Parameter(Mandatory = $true, HelpMessage = "Source to copy from.")]
[ValidateNotNullOrEmpty()]
[string] $Path,
[Parameter(Mandatory = $true, HelpMessage = "Destination to copy to.")]
[ValidateNotNullOrEmpty()]
[string] $Destination
);
if (Test-Path -Path "${Path}" -PathType Container) {
$Path = "${Path}\*";
};
$fof_createprogressdlg = "&H0&";
$shellApplication = New-Object -ComObject "Shell.Application";
$destLocationFolder = $shellApplication.NameSpace($Destination);
$destLocationFolder.CopyHere($Path, $fof_createprogressdlg);
};
function Get-P11GuiFileNameDialog {
<#
.SYNOPSIS
Opens a gui file-picker dialog.
#>
[CmdletBinding()] [OutputType([string])]
param(
[Parameter(Mandatory = $false, HelpMessage = "Directory path to open the dialog in. Default: ScriptRoot.")]
[string] $InitialDirectory = "${PSScriptRoot}",
[Parameter(Mandatory = $false, HelpMessage = "File type filter string. Default: `"ISO (*.iso) | *.iso`"")]
[string] $Filter = "ISO (*.iso) | *.iso"
);
if (($null -eq $InitialDirectory) -or ($InitialDirectory -eq "")) {
$InitialDirectory = "${PSScriptRoot}"
}
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog;
$OpenFileDialog.Title = "Please select a Windows installer iso."
$OpenFileDialog.InitialDirectory = "${InitialDirectory}";
$OpenFileDialog.Filter = "${Filter}";
$dialogResult = $OpenFileDialog.ShowDialog();
if ($dialogResult.ToString() -eq "Cancel") {
return $null;
}
else {
[string] $fullIsoPath = $OpenFileDialog.FileName;
if (($null -eq $fullIsoPath) -or (-not($fullIsoPath.EndsWith(".iso"))) -or (-not(Test-Path -Path "${fullIsoPath}"))) {
throw "Invalid ISO! '${fullIsoPath}'"
};
return $fullIsoPath;
};
};
function Mount-P11WindowsIso {
<#
.SYNOPSIS
Mounts the source ISO.
#>
[CmdletBinding()] [OutputType([System.Void])] param();
$isoImage = (Get-DiskImage -ImagePath "$($P11State.IsoPath)");
if (-not ($isoImage.Attached)) {
Write-Log -Message "Mounting ISO `"$($P11State.IsoPath)`"" -Severity Debug;
Mount-DiskImage -ImagePath "$($P11State.IsoPath)" -ErrorAction Stop | Out-Null;
};
$isoDrive = $(Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType = 5" | Where-Object { $_.Size -eq $isoImage.Size }).DeviceID;
$isoBootWim = "${isoDrive}\sources\boot.wim";
# $isoBootWim = Join-Path -Path "${isoDrive}" -ChildPath "sources" -AdditionalChildPath "boot.wim";
if (Test-Path -Path "${isoBootWim}" -PathType Leaf) {
$script:P11State.IsoDrive = "${isoDrive}";
Write-Log -Message "ISO mounted as drive $($P11State.IsoDrive)" -Severity Info;
}
else {
throw "Invalid ISO ('${isoBootWim} not found') or Failed to mount!";
};
};
function Dismount-P11WindowsIso {
<#
.SYNOPSIS
Dismounts the source iso.
#>
[CmdletBinding()] [OutputType([System.Void])] param();
$isoImage = (Get-DiskImage -ImagePath "$($P11State.IsoPath)");
if ($isoImage.Attached) {
Dismount-DiskImage -ImagePath "$($P11State.IsoPath)" -ErrorAction Stop | Out-Null;
Write-Log -Message "Dismounted ISO from drive $($P11State.IsoDrive)" -Severity Info;
};
};
#endregion
#region Low level BuildIso functions
function Add-P11HostsFileBlocks {
<#
.SYNOPSIS
Populates the hosts file with goodies from .\config\hosts.conf
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Adding hosts file blocks..." -Severity Info;
$P11Config.HostsToBlock | ForEach-Object {
Add-P11HostsFileBlockEntry -Domain $_;
};
};
function Remove-P11AppxProvisionedPackages {
<#
.SYNOPSIS
Removes AppX packages defined in .\config\appx.conf (if they exists on the image)
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Removing AppX packages..." -Severity Info;
$GetAppxProvisionedPackageArgs = Get-P11DismBaseArgs -WithPath;
[array] $detectedAppxProvisionedPackages = (Get-AppxProvisionedPackage @GetAppxProvisionedPackageArgs | Select-Object -Property DisplayName, PackageName, PublisherId, InstallLocation);
foreach ($detectedAppxProvisionedPackage in $detectedAppxProvisionedPackages) {
Write-Log -Message "Detected AppX Package: $($detectedAppxProvisionedPackage.DisplayName)" -Severity Debug;
};
[array] $flaggedForRemoval = @();
foreach ($detectedPackage in $detectedAppxProvisionedPackages) {
foreach ($removablePackage in $P11Config.AppxPackagesToRemove) {
if ($detectedPackage.DisplayName -contains $removablePackage) {
$flaggedForRemoval += $detectedPackage;
};
};
};
foreach ($package in $flaggedForRemoval) {
Write-Log -Message "Removing AppX package: $($package.DisplayName)" -Severity Debug;
$RemoveAppxProvisionedPackageArgs = Get-P11DismBaseArgs -WithPath;
$RemoveAppxProvisionedPackageArgs.PackageName = "$($package.PackageName)";
$RemoveAppxProvisionedPackageArgs.ErrorAction = "Continue";
Remove-AppxProvisionedPackage @RemoveAppxProvisionedPackageArgs | Out-Null;
};
Mount-P11RegistryHive -Software;
foreach ($package in $flaggedForRemoval) {
[string] $installPath = $package.InstallLocation.ToString();
[string] $installPath = $installPath.Replace("%SYSTEMDRIVE%", "$($P11Paths.MountDirectory)");
[string] $fullInstallPath = "$(([IO.FileInfo] $installPath).Directory.Parent.FullName)";
Remove-Item -Path "${fullInstallPath}\*" -Recurse -Force -ErrorAction SilentlyContinue | Out-Null;
# Mark the package as Deprovisioned and EndOfLife to prevent reinstallation.
$(& "reg" "add" "HKLM\P11_SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\Deprovisioned\$($package.DisplayName)_$($package.PublisherId)" "/f" 2>&1) | Out-Null;
# Adding this line breaks windows updates
# $(& "reg" "add" "HKLM\P11_SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\EndOfLife\S-1-1-0\$($package.DisplayName)_$($package.PublisherId)" "/f" 2>&1) | Out-Null;
};
Dismount-P11RegistryHives;
};
function Disable-P11WindowsOptionalFeature {
<#
.SYNOPSIS
Disables Optional Features defined in .\config\features.conf (if they exists on the image)
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Removing Optional Features..." -Severity Info;
$GetWindowsOptionalFeatureArgs = Get-P11DismBaseArgs -WithPath;
[array] $detectedOptionalFeatures = (Get-WindowsOptionalFeature @GetWindowsOptionalFeatureArgs | Where-Object { $_.State -eq 'Enabled' } | Select-Object -Property FeatureName).FeatureName;
foreach ($feature in $detectedOptionalFeatures) {
Write-Log -Message "Detected Optional Feature: ${feature}" -Severity Debug;
};
foreach ($feature in $detectedOptionalFeatures) {
if ($P11Config.FeaturesToRemove -contains $feature) {
$DisableWindowsOptionalFeatureArgs = Get-P11DismBaseArgs -WithPath;
$DisableWindowsOptionalFeatureArgs.FeatureName = "${feature}";
$DisableWindowsOptionalFeatureArgs.ErrorAction = "Continue";
Write-Log -Message "Removing Feature: ${feature}" -Severity Debug;
Disable-WindowsOptionalFeature @DisableWindowsOptionalFeatureArgs | Out-Null;
};
};
};
function Remove-P11WindowsCapability {
<#
.SYNOPSIS
Removes Windows Capabilities defined in .\config\capabilities.conf (if they exists on the image)
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Removing Windows Capabilities..." -Severity Info;
$GetWindowsCapabilityArgs = Get-P11DismBaseArgs -WithPath;
$GetWindowsCapabilityArgs.LimitAccess = $true;
[array] $detectedCapabilities = (Get-WindowsCapability @GetWindowsCapabilityArgs | Where-Object { $_.State -ne 'NotPresent' } | Select-Object -Property Name).Name;
foreach ($detectedCapability in $detectedCapabilities) {
Write-Log -Message "Detected Windows Capability: $($detectedCapability)" -Severity Debug;
};
foreach ($detectedCapability in $detectedCapabilities) {
foreach ($removableCapability in $P11Config.CapabilitiesToRemove) {
if ($detectedCapability -like "*${removableCapability}*") {
$RemoveWindowsCapabilityArgs = Get-P11DismBaseArgs -WithPath;
$RemoveWindowsCapabilityArgs.Name = "${detectedCapability}";
Write-Log -Message "Removing Windows Capability: ${detectedCapability}" -Severity Debug;
Remove-WindowsCapability @RemoveWindowsCapabilityArgs | Out-Null;
};
};
};
};
function Remove-P11FileSystemBloat {
<#
.SYNOPSIS
Removes Files and Folders defined in .\config\files.txt (if they exists on the image)
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Removing Files and Folders..." -Severity Info;
foreach ($removablePath in $P11Config.PathsToRemove) {
$resolvedPath = (Resolve-Path -Path "${removablePath}" -ErrorAction SilentlyContinue).Path;
if ($null -ne $resolvedPath) {
if ($resolvedPath.GetType().Name -eq "Array") {
foreach ($path in $resolvedPath) {
if (Test-Path -Path "${path}" -PathType Container) {
Write-Log -Message "Removing Directory: ${path}" -Severity Debug;
Remove-Item -Path "${path}" -Recurse -Force -ErrorAction SilentlyContinue | Out-Null;
}
elseif (Test-Path -Path "${path}" -PathType Leaf) {
Write-Log -Message "Removing File: ${path}" -Severity Debug;
Remove-Item -Path "${path}" -Force -ErrorAction SilentlyContinue | Out-Null;
};
};
}
else {
if (Test-Path -Path "${resolvedPath}" -PathType Container) {
Write-Log -Message "Removing Directory: ${resolvedPath}" -Severity Debug;
Remove-Item -Path "${resolvedPath}" -Recurse -Force -ErrorAction SilentlyContinue | Out-Null;
}
elseif (Test-Path -Path "${resolvedPath}" -PathType Leaf) {
Write-Log -Message "Removing File: ${resolvedPath}" -Severity Debug;
Remove-Item -Path "${resolvedPath}" -Force -ErrorAction SilentlyContinue | Out-Null;
};
};
};
};
};
function Invoke-P11ManualTweaks {
<#
.SYNOPSIS
Rigs certain stubborn software like:
- Edge
- Microsoft.OutlookForWindows
- Microsoft.Windows.DevHome
- MicrosoftWindows.CrossDevice
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Deploying manual tweaking for certain apps..." -Severity Info;
Mount-P11RegistryHive -Software;
# Edge
$edgeExeFolder = "$($P11Paths.MountDirectory)\Windows\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe";
$edgeExePath = "${edgeExeFolder}\MicrosoftEdge.exe";
New-Item -Path "${edgeExeFolder}" -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null;
Set-Content -Path "${edgeExePath}" -Value "[Pure11] We don't appreciate Microsoft junk here!" -Force | Out-Null;
$(& "reg" "add" "HKLM\P11_SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\Deprovisioned\Microsoft.MicrosoftEdge_8wekyb3d8bbwe" "/f" 2>&1) | Out-Null;
# Adding this line breaks windows updates
# $(& "reg" "add" "HKLM\P11_SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\EndOfLife\S-1-1-0\Microsoft.MicrosoftEdge_8wekyb3d8bbwe" "/f" 2>&1) | Out-Null;
# Microsoft.OutlookForWindows
$(& "reg" "add" "HKLM\P11_SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\Deprovisioned\Microsoft.OutlookForWindows_8wekyb3d8bbwe" "/f" 2>&1) | Out-Null;
# Adding this line breaks windows updates
# $(& "reg" "add" "HKLM\P11_SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\EndOfLife\S-1-1-0\Microsoft.OutlookForWindows_8wekyb3d8bbwe" "/f" 2>&1) | Out-Null;
# Microsoft.Windows.DevHome
$(& "reg" "add" "HKLM\P11_SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\Deprovisioned\Microsoft.Windows.DevHome_8wekyb3d8bbwe" "/f" 2>&1) | Out-Null;
# Adding this line breaks windows updates
# $(& "reg" "add" "HKLM\P11_SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\EndOfLife\S-1-1-0\Microsoft.Windows.DevHome_8wekyb3d8bbwe" "/f" 2>&1) | Out-Null;
# MicrosoftWindows.CrossDevice
$(& "reg" "add" "HKLM\P11_SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\Deprovisioned\MicrosoftWindows.CrossDevice_cw5n1h2txyewy" "/f" 2>&1) | Out-Null;
# Adding this line breaks windows updates
# $(& "reg" "add" "HKLM\P11_SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\EndOfLife\S-1-1-0\MicrosoftWindows.CrossDevice_cw5n1h2txyewy" "/f" 2>&1) | Out-Null;
Dismount-P11RegistryHives;
};
function Invoke-P11BypassSystemRequirements {
<#
.SYNOPSIS
Bypassing Windows 11 System requirements.
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Bypassing system requirements..." -Severity Info;
Mount-P11RegistryHive -Default -NTUSER -System;
$(& "reg" "import" "$($P11Paths.SysReqRegConfig)" 2>&1) | Out-Null;
Dismount-P11RegistryHives;
};
function Invoke-P11RegistryTweaks {
<#
.SYNOPSIS
Imports registry tweaks defined in .\config\reg\*.reg
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Importing registry tweaks..." -Severity Info;
Mount-P11RegistryHive -All;
$P11Config.RegistryFiles | ForEach-Object {
Write-Log -Message "Importing registry tweak: $($_)" -Severity Debug;
$(& "reg" "import" "$($_)" 2>&1) | Out-Null;
};
Dismount-P11RegistryHives;
};
function Install-P11Drivers {
<#
.SYNOPSIS
Imports drivers placed to .\work\drivers
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Importing drivers..." -Severity Info;
$AddWindowsDriverArgs = Get-P11DismBaseArgs -WithPath;
$AddWindowsDriverArgs.Driver = "$($P11Paths.DriversDirectory)";
$AddWindowsDriverArgs.Recurse = $true;
Add-WindowsDriver @AddWindowsDriverArgs | Out-Null;
};
function Install-P11Updates {
<#
.SYNOPSIS
Imports windows updates placed to .\work\updates
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Importing windows updates..." -Severity Info;
$AddWindowsPackageArgs = Get-P11DismBaseArgs -WithPath;
$AddWindowsPackageArgs.PackagePath = "$($P11Paths.UpdatesDirectory)";
$AddWindowsPackageArgs.IgnoreCheck = $true;
};
#endregion
#region High level BuildIso functions
function Copy-P11IsoToSourceCache {
<#
.SYNOPSIS
Populates the [work\iso-source] directory with the install iso contents.
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Caching ISO contents..." -Severity Info;
Mount-P11WindowsIso;
Copy-P11GuiRecursiveWithProgress -Path "$($P11State.IsoDrive)" -Destination "$($P11Paths.IsoSourceDirectory)";
Dismount-P11WindowsIso;
Write-Log -Message "ISO contents cached." -Severity Info;
};
function Select-P11WindowsIso {
<#
.SYNOPSIS
Prompts the user to pick a source ISO.
#>
[CmdletBinding()] [OutputType([System.Void])] param();
$selectedIso = Get-P11GuiFileNameDialog -InitialDirectory "$($Env:P11_ISOPATH)";
if ($null -eq $selectedIso) {
throw "No ISO selected!";
};
if (-not(Test-Path -Path "${selectedIso}" -PathType Leaf)) {
throw "Source ISO not found!";
};
$Env:P11_ISOPATH = (Get-Item -Path "${selectedIso}").Directory.FullName;
$Script:P11State.IsoPath = "${selectedIso}";
Write-Log -Message "Selected source ISO: $($P11State.IsoPath)" -Severity Info;
};
function Select-P11Sku {
<#
.SYNOPSIS
Reads the available images from install.wim and prompts the user to pick one.
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Loading available SKU images..." -Severity Info;
[hashtable] $GetWindowsImageArgs = Get-P11DismBaseArgs -InstallWim;
[array] $detectedWindowsImages = (Get-WindowsImage @GetWindowsImageArgs | Select-Object -Property ImageName).ImageName;
$Script:P11State.DetectedWindowsImages = $detectedWindowsImages;
if ($P11State.ImageName -eq [string]::Empty) {
Write-Host "";
$index = 0;
foreach ($image in $detectedWindowsImages) {
Write-Host "[${index}] ${image}" -ForegroundColor DarkBlue;
$index ++;
};
Write-Host "";
if ($($index - 1) -ge 1) {
[int] $selectedImageIndex = [int](Read-Host "Please select the SKU (0-$($index-1)) to customize");
}
else {
[int] $selectedImageIndex = 0;
};
[string] $Script:P11State.ImageName = $detectedWindowsImages[$selectedImageIndex];
};
Write-Log -Message "Selected Image is: $($P11State.ImageName)" -Severity Info;
};
function Remove-P11UnusedSku {
<#
.SYNOPSIS
Removes all SKU from install.wim with the exception of the selected one from (Select-P11Sku).
#>
[CmdletBinding()] [OutputType([System.Void])] param();
if ($P11State.DetectedWindowsImages.Count -gt 1) {
Write-Log -Message "Removing unused SKU images..." -Severity Info;
foreach ($image in ($P11State.DetectedWindowsImages | Where-Object { $_ -ne "Windows 11 Enterprise" })) {
Write-Log -Message "Removing SKU: ${image}" -Severity Debug;
$RemoveWindowsImageArgs = Get-P11DismBaseArgs -InstallWim;
$RemoveWindowsImageArgs.Name = "${image}";
Remove-WindowsImage @RemoveWindowsImageArgs | Out-Null;
};
};
Get-P11WindowsImageInfo;
};
function Deploy-P11AutounattendXml {
<#
.SYNOPSIS
Copies the autounattend.xml to the iso root.
#>
[CmdletBinding()] [OutputType([System.Void])] param();
Write-Log -Message "Deploying autounattend.xml..." -Severity Info;
$localizedAutounattendXml = "$($P11Paths.ConfigDirectory)\autounattend.$($P11State.Language).xml";
if (Test-Path -Path "${localizedAutounattendXml}") {
Write-Log -Message "Using Localized autounattend.$($P11State.Language).xml..." -Severity Info;
Copy-Item -Path "${localizedAutounattendXml}" -Destination "$($P11Paths.AutounattendXmlIso)" -Force -ErrorAction SilentlyContinue | Out-Null;
}
else {
Copy-Item -Path "$($P11Paths.AutounattendXml)" -Destination "$($P11Paths.AutounattendXmlIso)" -Force -ErrorAction SilentlyContinue | Out-Null;
};
};