Skip to content

Commit

Permalink
Avoid repeated loading and unloading of ‘C:\Users\Default\NTUSER.DAT’…
Browse files Browse the repository at this point in the history
… hive
  • Loading branch information
cschneegans committed Nov 14, 2024
1 parent 5178e23 commit 7289c5b
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 159 deletions.
33 changes: 28 additions & 5 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ public void RestartExplorer()
public string GetScript()
{
StringWriter writer = new();

void WriteScriptBlock(string command)
{
writer.WriteLine("\t{");
Expand All @@ -443,6 +444,7 @@ void WriteScriptBlock(string command)
}
writer.WriteLine("\t};");
}

writer.WriteLine("$scripts = @(");
foreach (string command in commands)
{
Expand Down Expand Up @@ -471,7 +473,7 @@ void WriteScriptBlock(string command)
/// <summary>
/// Collects PowerShell commands that will be run whenever a user logs on for the first time.
/// </summary>
public class UserOnceScriptSequence : PowerShellSequence
public class UserOnceSequence : PowerShellSequence
{
protected override string Activity()
{
Expand Down Expand Up @@ -500,6 +502,22 @@ protected override string LogFile()
}
}

/// <summary>
/// Collects PowerShell commands that modify the default user's registry hive.
/// </summary>
public class DefaultUserSequence : PowerShellSequence
{
protected override string Activity()
{
return "Running scripts to modify the default user’’s registry hive.";
}

protected override string LogFile()
{
return @"C:\Windows\Setup\Scripts\DefaultUser.log";
}
}

public interface IKeyed
{
string Id { get; }
Expand Down Expand Up @@ -910,7 +928,8 @@ public XmlDocument GenerateXml(Configuration config)
NamespaceManager: ns,
Generator: this,
FirstLogonScript: new FirstLogonSequence(),
UserOnceScript: new UserOnceScriptSequence()
UserOnceScript: new UserOnceSequence(),
DefaultUserScript: new DefaultUserSequence()
);

new List<Modifier> {
Expand All @@ -932,8 +951,9 @@ public XmlDocument GenerateXml(Configuration config)
new TimeZoneModifier(context),
new WdacModifier(context),
new ScriptModifier(context),
new FirstLogonModifier(context),
new UserOnceModifier(context),
new DefaultUserModifier(context),
new FirstLogonModifier(context),
new OrderModifier(context),
new ProcessorArchitectureModifier(context),
new PrettyModifier(context),
Expand Down Expand Up @@ -982,7 +1002,8 @@ public record class ModifierContext(
Configuration Configuration,
UnattendGenerator Generator,
FirstLogonSequence FirstLogonScript,
UserOnceScriptSequence UserOnceScript
UserOnceSequence UserOnceScript,
DefaultUserSequence DefaultUserScript
);

abstract class Modifier(ModifierContext context)
Expand All @@ -997,7 +1018,9 @@ abstract class Modifier(ModifierContext context)

public FirstLogonSequence FirstLogonScript { get; } = context.FirstLogonScript;

public UserOnceScriptSequence UserOnceScript { get; } = context.UserOnceScript;
public UserOnceSequence UserOnceScript { get; } = context.UserOnceScript;

public DefaultUserSequence DefaultUserScript { get; } = context.DefaultUserScript;

public XmlElement NewSimpleElement(string name, XmlElement parent, string innerText)
{
Expand Down
24 changes: 4 additions & 20 deletions modifier/Bloatware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -192,25 +193,15 @@ public override void Process()
CommandBuilder.ShellCommand(@"del ""C:\Windows\System32\OneDriveSetup.exe"""),
CommandBuilder.ShellCommand(@"del ""C:\Windows\SysWOW64\OneDriveSetup.exe"""),
]);
appender.Append(
CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) =>
{
return [CommandBuilder.RegistryCommand(@$"delete ""{rootKey}\{subKey}\Software\Microsoft\Windows\CurrentVersion\Run"" /v OneDriveSetup /f")];
})
);
DefaultUserScript.Append(@"Remove-ItemProperty -LiteralPath 'Registry::HKU\DefaultUser\Software\Microsoft\Windows\CurrentVersion\Run' -Name 'OneDriveSetup' -Force -ErrorAction 'Continue';");
break;
case CustomBloatwareStep when bw.Id == "RemoveTeams":
appender.Append(
CommandBuilder.RegistryCommand(@"add ""HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Communications"" /v ConfigureChatAutoInstall /t REG_DWORD /d 0 /f")
);
break;
case CustomBloatwareStep when bw.Id == "RemoveNotepad":
appender.Append(
CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) =>
{
return [CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\Software\Microsoft\Notepad"" /v ShowStoreBanner /t REG_DWORD /d 0 /f")];
})
);
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Microsoft\Notepad"" /v ShowStoreBanner /t REG_DWORD /d 0 /f;");
break;
case CustomBloatwareStep when bw.Id == "RemoveOutlook":
appender.Append(
Expand All @@ -224,14 +215,7 @@ public override void Process()
break;
case CustomBloatwareStep when bw.Id == "RemoveCopilot":
UserOnceScript.Append("Get-AppxPackage -Name 'Microsoft.Windows.Ai.Copilot.Provider' | Remove-AppxPackage;");
appender.Append(
CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) =>
{
return [
CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\Software\Policies\Microsoft\Windows\WindowsCopilot"" /v TurnOffWindowsCopilot /t REG_DWORD /d 1 /f")
];
})
);
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Policies\Microsoft\Windows\WindowsCopilot"" /v TurnOffWindowsCopilot /t REG_DWORD /d 1 /f;");
break;
default:
throw new NotSupportedException();
Expand Down
20 changes: 20 additions & 0 deletions modifier/DefaultUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Schneegans.Unattend;

class DefaultUserModifier(ModifierContext context) : Modifier(context)
{
public override void Process()
{
if (DefaultUserScript.IsEmpty)
{
return;
}
CommandAppender appender = GetAppender(CommandConfig.Specialize);
string script = DefaultUserScript.GetScript();
string ps1File = @"C:\Windows\Setup\Scripts\DefaultUser.ps1";
AddTextFile(script, ps1File);

appender.Append(CommandBuilder.RegistryCommand(@"load ""HKU\DefaultUser"" ""C:\Users\Default\NTUSER.DAT"""));
appender.Append(CommandBuilder.InvokePowerShellScript(ps1File));
appender.Append(CommandBuilder.RegistryCommand(@"unload ""HKU\DefaultUser"""));
}
}
149 changes: 53 additions & 96 deletions modifier/Optimizations.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;

namespace Schneegans.Unattend;
Expand Down Expand Up @@ -71,28 +69,22 @@ public override void Process()
{
CommandAppender appender = GetAppender(CommandConfig.Specialize);

if (Configuration.ShowFileExtensions)
{
IEnumerable<string> SetExplorerOptions(string rootKey, string subKey)
{
if (Configuration.ShowFileExtensions)
{
yield return CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced"" /v ""HideFileExt"" /t REG_DWORD /d 0 /f");
}
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"" /v ""HideFileExt"" /t REG_DWORD /d 0 /f;");
}

switch (Configuration.HideFiles)
{
case HideModes.None:
yield return CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced"" /v ""Hidden"" /t REG_DWORD /d 1 /f");
yield return CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced"" /v ""ShowSuperHidden"" /t REG_DWORD /d 1 /f");
break;
case HideModes.HiddenSystem:
yield return CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced"" /v ""Hidden"" /t REG_DWORD /d 1 /f");
break;
case HideModes.Hidden:
break;
}
}
appender.Append(CommandBuilder.RegistryDefaultUserCommand(SetExplorerOptions));
switch (Configuration.HideFiles)
{
case HideModes.None:
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"" /v ""Hidden"" /t REG_DWORD /d 1 /f;");
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"" /v ""ShowSuperHidden"" /t REG_DWORD /d 1 /f;");
break;
case HideModes.HiddenSystem:
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"" /v ""Hidden"" /t REG_DWORD /d 1 /f;");
break;
case HideModes.Hidden:
break;
}

if (Configuration.DisableWindowsUpdate)
Expand All @@ -115,9 +107,7 @@ IEnumerable<string> SetExplorerOptions(string rootKey, string subKey)
string ps1File = @"C:\Windows\Setup\Scripts\ShowAllTrayIcons.ps1";
string script = Util.StringFromResource("ShowAllTrayIcons.ps1");
AddTextFile(script, ps1File);
appender.Append(
CommandBuilder.InvokePowerShellScript(ps1File)
);
DefaultUserScript.InvokeFile(ps1File);
AddXmlFile(Util.XmlDocumentFromResource("ShowAllTrayIcons.xml"), @"C:\Windows\Setup\Scripts\ShowAllTrayIcons.xml");
AddTextFile(Util.StringFromResource("ShowAllTrayIcons.vbs"), @"C:\Windows\Setup\Scripts\ShowAllTrayIcons.vbs");
}
Expand All @@ -133,14 +123,7 @@ IEnumerable<string> SetExplorerOptions(string rootKey, string subKey)

if (Configuration.HideTaskViewButton)
{
appender.Append(
CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) =>
{
return [
CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced"" /v ShowTaskViewButton /t REG_DWORD /d 0 /f"),
];
})
);
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"" /v ShowTaskViewButton /t REG_DWORD /d 0 /f;");
}

if (Configuration.DisableDefender)
Expand Down Expand Up @@ -168,16 +151,11 @@ IEnumerable<string> SetExplorerOptions(string rootKey, string subKey)
CommandBuilder.RegistryCommand(@"add ""HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WTDS\Components"" /v NotifyMalicious /t REG_DWORD /d 0 /f"),
CommandBuilder.RegistryCommand(@"add ""HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WTDS\Components"" /v NotifyPasswordReuse /t REG_DWORD /d 0 /f"),
CommandBuilder.RegistryCommand(@"add ""HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WTDS\Components"" /v NotifyUnsafeApp /t REG_DWORD /d 0 /f"),
..CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) =>
{
return [
CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\Software\Microsoft\Edge\SmartScreenEnabled"" /ve /t REG_DWORD /d 0 /f"),
CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\Software\Microsoft\Edge\SmartScreenPuaEnabled"" /ve /t REG_DWORD /d 0 /f"),
CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\Software\Microsoft\Windows\CurrentVersion\AppHost"" /v EnableWebContentEvaluation /t REG_DWORD /d 0 /f"),
CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\Software\Microsoft\Windows\CurrentVersion\AppHost"" /v PreventOverride /t REG_DWORD /d 0 /f"),
];
})
]);
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Microsoft\Edge\SmartScreenEnabled"" /ve /t REG_DWORD /d 0 /f;");
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Microsoft\Edge\SmartScreenPuaEnabled"" /ve /t REG_DWORD /d 0 /f;");
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Microsoft\Windows\CurrentVersion\AppHost"" /v EnableWebContentEvaluation /t REG_DWORD /d 0 /f;");
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Microsoft\Windows\CurrentVersion\AppHost"" /v PreventOverride /t REG_DWORD /d 0 /f;");
}

if (Configuration.DisableUac)
Expand Down Expand Up @@ -273,18 +251,10 @@ IEnumerable<string> SetExplorerOptions(string rootKey, string subKey)

if (Configuration.TurnOffSystemSounds)
{
appender.Append(
CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) =>
{
StringWriter writer = new();
writer.WriteLine(@$"$mountKey = '{rootKey}\{subKey}';");
writer.WriteLine(Util.StringFromResource("TurnOffSystemSounds.ps1"));
string ps1File = @"%TEMP%\TurnOffSystemSounds.ps1";
AddTextFile(writer.ToString(), ps1File);
return [
CommandBuilder.InvokePowerShellScript(ps1File),
];
}));
string ps1File = @"C:\Windows\Setup\Scripts\TurnOffSystemSounds.ps1";
string script = Util.StringFromResource("TurnOffSystemSounds.ps1");
AddTextFile(script, ps1File);
DefaultUserScript.InvokeFile(ps1File);
UserOnceScript.Append(@"Set-ItemProperty -LiteralPath 'Registry::HKCU\AppEvents\Schemes' -Name '(Default)' -Type 'String' -Value '.None';");

appender.Append([
Expand All @@ -297,33 +267,29 @@ IEnumerable<string> SetExplorerOptions(string rootKey, string subKey)
{
// https://skanthak.homepage.t-online.de/ten.html#eighth

appender.Append(
CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) =>
{
return new List<string>()
{
"ContentDeliveryAllowed",
"FeatureManagementEnabled",
"OEMPreInstalledAppsEnabled",
"PreInstalledAppsEnabled",
"PreInstalledAppsEverEnabled",
"SilentInstalledAppsEnabled",
"SoftLandingEnabled",
"SubscribedContentEnabled",
"SubscribedContent-310093Enabled",
"SubscribedContent-338387Enabled",
"SubscribedContent-338388Enabled",
"SubscribedContent-338389Enabled",
"SubscribedContent-338393Enabled",
"SubscribedContent-353698Enabled",
"SystemPaneSuggestionsEnabled",
}.Select(value =>
{
return CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager"" /v ""{value}"" /t REG_DWORD /d 0 /f");
});
})
);
DefaultUserScript.Append("""
$names = @(
'ContentDeliveryAllowed';
'FeatureManagementEnabled';
'OEMPreInstalledAppsEnabled';
'PreInstalledAppsEnabled';
'PreInstalledAppsEverEnabled';
'SilentInstalledAppsEnabled';
'SoftLandingEnabled';
'SubscribedContentEnabled';
'SubscribedContent-310093Enabled';
'SubscribedContent-338387Enabled';
'SubscribedContent-338388Enabled';
'SubscribedContent-338389Enabled';
'SubscribedContent-338393Enabled';
'SubscribedContent-353698Enabled';
'SystemPaneSuggestionsEnabled';
);

foreach( $name in $names ) {
reg.exe add "HKU\DefaultUser\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" /v $name /t REG_DWORD /d 0 /f;
}
""");
appender.Append(
CommandBuilder.RegistryCommand(@"add ""HKLM\Software\Policies\Microsoft\Windows\CloudContent"" /v ""DisableWindowsConsumerFeatures"" /t REG_DWORD /d 0 /f")
);
Expand Down Expand Up @@ -379,12 +345,7 @@ IEnumerable<string> SetExplorerOptions(string rootKey, string subKey)

if (Configuration.LeftTaskbar)
{
appender.Append(
CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) =>
{
return [CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced"" /v TaskbarAl /t REG_DWORD /d 0 /f")];
})
);
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"" /v TaskbarAl /t REG_DWORD /d 0 /f;");
}

if (Configuration.HideEdgeFre)
Expand All @@ -411,16 +372,12 @@ IEnumerable<string> SetExplorerOptions(string rootKey, string subKey)
{
indicators |= 4;
}
appender.Append(
CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) =>
{
return [
CommandBuilder.RegistryCommand(@$"add ""HKU\.DEFAULT\Control Panel\Keyboard"" /v InitialKeyboardIndicators /t REG_SZ /d ""{indicators}"" /f"),
CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\Control Panel\Keyboard"" /v InitialKeyboardIndicators /t REG_SZ /d ""{indicators}"" /f")
];
}
)
);

DefaultUserScript.Append($$"""
foreach( $root in 'Registry::HKU\.DEFAULT', 'Registry::HKU\DefaultUser' ) {
Set-ItemProperty -LiteralPath "$root\Control Panel\Keyboard" -Name 'InitialKeyboardIndicators' -Type 'String' -Value {{indicators}} -Force;
}
""");
}
{
bool ignoreCapsLock = settings.CapsLock.Behavior == KeyBehavior.Ignore;
Expand Down
9 changes: 1 addition & 8 deletions modifier/Personalization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,7 @@ public override void Process()
""");
writer.WriteLine(script);
AddTextFile(writer.ToString(), ps1File);
appender.Append(
CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) =>
{
return [
CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\SOFTWARE\Microsoft\Windows\DWM"" /v ColorPrevalence /t REG_DWORD /d {(settings.AccentColorOnBorders ? 1 : 0)} /f"),
];
})
);
DefaultUserScript.Append(@$"reg.exe add ""HKU\DefaultUser\Software\Microsoft\Windows\DWM"" /v ColorPrevalence /t REG_DWORD /d {(settings.AccentColorOnBorders ? 1 : 0)} /f;");
UserOnceScript.InvokeFile(ps1File);
UserOnceScript.RestartExplorer();
}
Expand Down
Loading

0 comments on commit 7289c5b

Please sign in to comment.