So I have a multi-part script that I am trying to implement to make configuring new installs of Windows 11 23H2 cookie cutter. The basic outline of the script is as follows:
Enable Built-In Administrator account and set its password
Enable auto login to Administrator when computer is restarted
set the Timezone to CST
Rename the computer to the BIOS Serial number
change system and user environment variables to a new folder called C:Temp
set C:Temp networksvc permissions to full access
Redirect all Browser temp files to C:TempUSERNAMETemporary Internet Files
Set the Power settings using powerCFG
When logged into Administrator, delete the initial user and its folder
Force all applicable MS updates until all updates are complete
Turn off the auto login and clean up any files created
Below is the slightly modified version of the script I have so far. any advice on how to optimize/ debug?
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
$mypath = $MyInvocation.MyCommand.Path
$mydirectory = Split-Path -path $mypath -parent
set-location $mydirectory
Used for Recurse based on the presence of the ‘Admin’ Profile and user folder
if(Test-Path -Path ‘C:UsersAdmin’){
## Enable 'Administrator' and set Password ##
if (!(Get-LocalUser -Name "Administrator").Enabled){
$adminPassword = ConvertTo-SecureString -AsPlainText 'Password' -Force
Enable-LocalUser -Name "Administrator"
Set-LocalUser -Name "Administrator" -Password $adminPassword
## Set Auto-Login as Administrator and reboot"
Set-ItemProperty -path "HKLM:SOFTWAREMicrosoftWindows NTCurrentVersionWinlogon" -name "DefaultUserName" -Value "Administrator" -Type "String"
Set-ItemProperty -path "HKLM:SOFTWAREMicrosoftWindows NTCurrentVersionWinlogon" -name "DefaultPassword" -Value 'Password' -Type "String"
Set-ItemProperty -path "HKLM:SOFTWAREMicrosoftWindows NTCurrentVersionWinlogon" -name "AutoAdminLogon" -Value "1"
Set-ItemProperty -path "HKLM:SOFTWAREMicrosoftWindowsCurrentVersionRun" -name "Step2" -Value $mydirectory"Generate.BAT"
## Set Timezone to CST ##
Set-TimeZone –ID "Central Standard Time"
## Reboot into Administrator with User Interaction ##
Write-Host "Administrator Enabled; Press Enter to Reboot."
Pause
Restart-Computer
}
Else{
## Rename Computer to Serial Number ##
$Serial = Get-WMIObject -Class "Win32_BIOS" | Select -Expand SerialNumber
Rename-Computer $Serial
## Create C:Temp and C:Temp ##
New-Item -Path "C:" -Name "Temp" -ItemType Directory
New-Item -Path "C:Temp$env:UserName" -Name "DirectoryName" -ItemType directory
## set permisions of 'C:Temp' to full access for Network SVC ##
icacls "C:Temp" /inheritance:d /grant:r networksvc:F /T
## Set System TEMP and TMP to C:Temp ##
$env:TEMP = "C:TEMP"
$env:TMP = "C:TEMP"
## set user temp variable to C:TempUser
Set-ItemProperty -Path "HKCU:Environment" -Name Temp -Value "C:Temp$env:UserName"
Set-ItemProperty -Path "HKCU:Environment" -Name Tmp -Value "C:Temp$env:UserName"
## redirect IE temp files to C:Temp'user'Temporary internet files
Set-ItemProperty -Path "HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet SettingsCache" -Name "Paths" -Value "C:Temp$env:UserNameTemporary Internet Files" -Type "String"
Set-ItemProperty -Path "HKCU:SoftwareMicrosoftWindowsCurrentVersionExplorerUser Shell Folders" -Name "Cache" -Value "C:Temp$env:UserNameTemporary Internet Files" -Type "String"
Set-ItemProperty -Path "HKCU:SoftwareMicrosoftWindowsCurrentVersionExplorerShell Folders" -Name "Cache" -Value "C:Temp$env:UserNameTemporary Internet Files" -Type "String"
Set-ItemProperty -Path "HKCU:SOFTWAREMicrosoftWindowsCurrentVersionInternet Settings5.0CacheContent" -Name "CacheLimit" -Value "40"
Set-ItemProperty -Path "HKCU:SOFTWAREMicrosoftWindowsCurrentVersionInternet Settings5.0Cache" -Name "ContentLimit" -Value "40"
## while in AC set screen saver to 15 hibernate to 0 sleep 0 standby to 0
Start-Process -FilePath "PowerCFG.bat"
## disable and delete hibernate
Set-ItemProperty -Path 'HKLM:SYSTEMCurrentControlSetControlPower' -name HibernateEnabledDefault -Value 0
## check if admin exists and delete admin user and profile
if (Get-LocalUser Admin){
Write-Host "Admin account found; Removing Account"
Remove-LocalUser -Name "Admin"
Remove-Item -Path "C:UsersAdmin" -recurse -Force
}
else{
if(Test-Path C:UsersAdmin){
Write-Host "No Account found for User, 'Admin'. Verifying no corresponding folder"
Remove-Item -Path "C:UsersAdmin" -recurse -Force
}
else{
Write-Host "No folder for 'Admin' Found"
}
}
Write-Host "Admin Removed/Not Present"
}
Restart-Computer
}
else{
force MS updates (Loop)
write-host (“Please click on ‘Yes to All’ if prompted”)
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
write-host (“Please click on ‘Yes to All’ if prompted”)
Install-Module PSWindowsUpdate
Add-WUServiceManager -MicrosoftUpdate
write-host (“Getting latest Microsoft updates…. please wait”)
Get-WindowsUpdate
write-host (“Installing updates, system will reboot when complete…”)
Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot
turn off auto login
Set-ItemProperty -path “HKLM:SOFTWAREMicrosoftWindows NTCurrentVersionWinlogon” -name “DefaultUserName” -Value “” -Type “String”
Remove-ItemProperty -path “HKLM:SOFTWAREMicrosoftWindows NTCurrentVersionWinlogon” -name “DefaultPassword”
Set-ItemProperty -path “HKLM:SOFTWAREMicrosoftWindows NTCurrentVersionWinlogon” -name “AutoAdminLogon” -Value “0”
Remove-ItemProperty -path “HKLM:SOFTWAREMicrosoftWindowsCurrentVersionRun” -name ‘Step2’
cleanup script delete C:Temp children run disk cleanup
Write-Host ‘Beginning Script Cleanup Procedure…’
Get-ChildItem -Path ‘C:temp’ -Recurse | Select -ExpandProperty FullName | Where {$_ -notlike ‘C:tempAdministrator’} | sort length -Descending | Remove-Item -force
Write-Host ‘Clearing CleanMgr.exe automation settings.’
Get-ItemProperty -Path ‘HKLM:SOFTWAREMicrosoftWindowsCurrentVersionExplorerVolumeCaches*’ -Name StateFlags0001 -ErrorAction SilentlyContinue | Remove-ItemProperty -Name StateFlags0001 -ErrorAction SilentlyContinue
Write-Host ‘Enabling Update Cleanup. This is done automatically in Windows 10 via a scheduled task.’
New-ItemProperty -Path ‘HKLM:SOFTWAREMicrosoftWindowsCurrentVersionExplorerVolumeCachesUpdate Cleanup’ -Name StateFlags0001 -Value 2 -PropertyType DWord
Write-Host ‘Enabling Temporary Files Cleanup.’
New-ItemProperty -Path ‘HKLM:SOFTWAREMicrosoftWindowsCurrentVersionExplorerVolumeCachesTemporary Files’ -Name StateFlags0001 -Value 2 -PropertyType DWord
Write-Host ‘Starting CleanMgr.exe…’
Start-Process -FilePath CleanMgr.exe -ArgumentList ‘/sagerun:1’ -WindowStyle Hidden -Wait
Write-Host ‘Waiting for CleanMgr and DismHost processes. Second wait neccesary as CleanMgr.exe spins off separate processes.’
Get-Process -Name cleanmgr,dismhost -ErrorAction SilentlyContinue | Wait-Process
$UpdateCleanupSuccessful = $false
if (Test-Path $env:SystemRootLogsCBSDeepClean.log) {
$UpdateCleanupSuccessful = Select-String -Path $env:SystemRootLogsCBSDeepClean.log -Pattern ‘Total size of superseded packages:’ -Quiet
}
if ($UpdateCleanupSuccessful) {
Write-Host ‘Rebooting to complete CleanMgr.exe Update Cleanup….’
pause
Restart-Computer ‘Rebooting to complete CleanMgr.exe Update Cleanup….’
}
}
Oh also There is a batch file that is setup to run the script whenever the computer is restarted.
I have been through several iterations of this script and at this point at a complete loss of how to improve it. There are some errors that occur, but I have been staring at the same answers on google to “SOLVE” them, but they never work out.
Ethan Brockwell GAMEManiac98 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.