Trying to create a script with powershell for configuration of a new windows install

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.

New contributor

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật