Okay, this is a multi-step process involving registry modifications. We'll need to:
- Modify the registry for the currently logged-on user (if any, and if the script is run by them or an admin).
- Modify the Default User profile's registry hive. This ensures any new users created after the script runs will get the setting.
- Modify the registry hives for all other existing user profiles on the machine that are not currently logged in.
The specific registry key for taskbar autohide is a bit tricky. It's stored in a binary value:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3
The value name is Settings. The 9th byte (index 8) of this binary data controls autohide:
02: Autohide OFF
03: Autohide ON
Important Considerations:
- Admin Privileges: This script must be run as an Administrator.
- Logged-in Users: Modifying the registry hive (
NTUSER.DAT) of a user who is currently logged in (other than the one running the script) is problematic because the file is in use. The script will attempt this, but it's most effective for users who are logged out. The change for currently logged-in users (other than the one running the script) will likely only apply after they log out and back in.
- Explorer Restart: For the change to take effect for the currently running user, Explorer needs to be restarted. Other users will see the change upon their next login.
- Error Handling: Basic error handling is included.
Here's the PowerShell script:
<#
.SYNOPSIS
Enables taskbar autohide for all current and future users on a standalone Windows 11 machine.
.DESCRIPTION
This script modifies the registry to enable taskbar autohide.
1. For the currently logged-in user running the script.
2. For the Default User profile (applies to new users).
3. For all other existing user profiles (applies when they next log in).
MUST BE RUN AS ADMINISTRATOR.
.NOTES
Author: Your Name/AI
Date: 2023-10-27
Version: 1.1
#>
#Requires -RunAsAdministrator
Write-Host "Starting script to enable taskbar autohide for all users..." -ForegroundColor Yellow
# --- Configuration ---
$regPathExplorer = "Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3"
$regValueName = "Settings"
$autoHideEnabledByte = [byte]0x03 # Autohide ON
$byteIndexToModify = 8 # 9th byte (0-indexed)
# --- Function to modify the StuckRects3 settings ---
function Set-TaskbarAutoHideInHive {
param (
[string]$RegistryHivePath, # e.g., "HKCU:", or "HKU:\TempHive"
[string]$UserDescription # For logging, e.g., "Current User", "Default User", "User SID"
)
$fullRegPath = Join-Path -Path $RegistryHivePath -ChildPath $regPathExplorer
Write-Host "Attempting to set autohide for: $UserDescription at $fullRegPath"
try {
if (-not (Test-Path $fullRegPath)) {
Write-Warning "Registry path $fullRegPath does not exist for $UserDescription. Skipping."
# Optional: Create the key if it doesn't exist (Explorer usually creates it)
# New-Item -Path $fullRegPath -Force | Out-Null
# Then you'd need to create a default 'Settings' value.
# For simplicity, we'll assume it exists if the user has logged in and used Explorer.
return
}
$settingsValue = Get-ItemProperty -Path $fullRegPath -Name $regValueName -ErrorAction SilentlyContinue
if (-not $settingsValue) {
Write-Warning " 'Settings' value not found under $fullRegPath for $UserDescription. Explorer might need to run once for this user."
# It's possible the 'Settings' value doesn't exist if Explorer hasn't fully initialized for this profile.
# A more robust script might create a default binary value here.
# For now, we'll skip if it's missing.
return
}
$binaryData = $settingsValue.$regValueName # Access the property dynamically
if ($binaryData -isnot [byte[]]) {
Write-Warning " 'Settings' value is not binary data for $UserDescription. Skipping."
return
}
if ($binaryData.Length -gt $byteIndexToModify) {
if ($binaryData[$byteIndexToModify] -ne $autoHideEnabledByte) {
Write-Host " Current 9th byte: $($binaryData[$byteIndexToModify] | Format-Hex -HideHeader). Modifying to enable autohide."
$binaryData[$byteIndexToModify] = $autoHideEnabledByte
Set-ItemProperty -Path $fullRegPath -Name $regValueName -Value $binaryData -Force
Write-Host " Successfully set autohide for $UserDescription." -ForegroundColor Green
} else {
Write-Host " Autohide already enabled for $UserDescription (9th byte is $($binaryData[$byteIndexToModify] | Format-Hex -HideHeader))." -ForegroundColor Cyan
}
} else {
Write-Warning " 'Settings' binary data for $UserDescription is too short (Length: $($binaryData.Length)). Cannot modify."
}
}
catch {
Write-Error "Error processing $UserDescription : $($_.Exception.Message)"
}
}
# --- 1. Current User ---
Write-Host "`n--- Processing Current User ---" -ForegroundColor Magenta
# Get current user's SID to avoid processing them twice if they are admin
$currentUserSID = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
Set-TaskbarAutoHideInHive -RegistryHivePath "HKCU:" -UserDescription "Current User ($currentUserSID)"
$restartExplorer = $true # Flag to restart Explorer later for current user
# --- 2. Default User (for future users) ---
Write-Host "`n--- Processing Default User Profile ---" -ForegroundColor Magenta
$defaultUserProfilePath = "$env:SystemDrive\Users\Default\NTUSER.DAT"
$tempHiveNameDefault = "TempDefaultUserHive" # Unique name for loading hive
if (Test-Path $defaultUserProfilePath) {
Write-Host "Loading Default User hive: $defaultUserProfilePath"
try {
reg load "HKU\$tempHiveNameDefault" $defaultUserProfilePath | Out-Null
Set-TaskbarAutoHideInHive -RegistryHivePath "HKU:\$tempHiveNameDefault" -UserDescription "Default User Profile"
}
catch {
Write-Error "Failed to load or process Default User hive: $($_.Exception.Message)"
}
finally {
# Ensure the hive is unloaded
[gc]::Collect() # Force garbage collection, sometimes helps with unloading
Start-Sleep -Seconds 1
Write-Host "Unloading Default User hive."
reg unload "HKU\$tempHiveNameDefault" | Out-Null
}
} else {
Write-Warning "Default User profile NTUSER.DAT not found at $defaultUserProfilePath."
}
# --- 3. All Other Existing User Profiles ---
Write-Host "`n--- Processing Other Existing User Profiles ---" -ForegroundColor Magenta
$profileListPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
Get-ChildItem -Path $profileListPath | ForEach-Object {
$sid = $_.PSChildName
# Filter out system profiles and profiles without a valid path, and skip current user
if (($sid -like "S-1-5-21-*") -and ($sid -ne $currentUserSID)) {
$profilePath = (Get-ItemProperty -Path $_.PSPath -Name "ProfileImagePath" -ErrorAction SilentlyContinue).ProfileImagePath
if ($profilePath -and (Test-Path $profilePath)) {
$ntuserDatPath = Join-Path -Path $profilePath -ChildPath "NTUSER.DAT"
$tempHiveNameUser = "TempUserHive_$($sid.Replace('-','_'))" # Create a unique temp hive name
if (Test-Path $ntuserDatPath) {
Write-Host "Processing profile for SID: $sid (Path: $profilePath)"
try {
# Attempt to load the user's hive. This will fail if the user is logged in.
reg load "HKU\$tempHiveNameUser" $ntuserDatPath | Out-Null
Write-Host " User hive loaded successfully (user likely logged off)."
Set-TaskbarAutoHideInHive -RegistryHivePath "HKU:\$tempHiveNameUser" -UserDescription "User SID $sid"
}
catch {
Write-Warning " Could not load hive for $sid (Path: $ntuserDatPath). User might be logged in or hive is otherwise inaccessible. Error: $($_.Exception.Message)"
# If user is logged in, their live HKCU might have been affected if they ARE the current user.
# Otherwise, the change might not stick for this specific logged-in user until their profile is re-processed when they are logged out.
}
finally {
# Ensure the hive is unloaded if it was loaded
if (Test-Path "HKU:\$tempHiveNameUser") {
[gc]::Collect()
Start-Sleep -Seconds 1
Write-Host " Unloading user hive for $sid."
reg unload "HKU\$tempHiveNameUser" | Out-Null
}
}
} else {
Write-Warning "NTUSER.DAT not found for SID $sid at $ntuserDatPath."
}
} else {
#Write-Host "Profile path not found or invalid for SID $sid. Skipping."
}
}
}
# --- Restart Explorer for the current user to apply settings ---
if ($restartExplorer) {
Write-Host "`n--- Applying changes for Current User ---" -ForegroundColor Magenta
$confirmation = Read-Host "Do you want to restart Explorer now to apply changes for the current user? (Y/N)"
if ($confirmation -eq 'Y' -or $confirmation -eq 'y') {
Write-Host "Restarting Explorer..."
try {
Get-Process explorer | Stop-Process -Force
# Explorer should restart automatically. If not, uncomment the next line:
# Start-Process explorer.exe
Write-Host "Explorer restarted." -ForegroundColor Green
} catch {
Write-Error "Failed to restart Explorer: $($_.Exception.Message)"
Write-Warning "You may need to log out and back in, or restart your computer for the changes to take full effect for the current user."
}
} else {
Write-Host "Explorer not restarted. Changes for the current user will apply after next login or manual Explorer restart."
}
}
Write-Host "`nScript finished. Taskbar autohide settings have been processed." -ForegroundColor Green
Write-Host "Changes for other users (not currently logged in) and new users will apply upon their next login."
How to Use:
- Save: Copy the code above into a new file named
Set-AutoHideTaskbar.ps1 on your USB drive.
- Run as Administrator:
- Navigate to the USB drive in File Explorer.
- Right-click
Set-AutoHideTaskbar.ps1.
- Select "Run with PowerShell".
- If prompted by UAC, click "Yes" to grant administrator privileges.
- Alternatively, open an administrative PowerShell window, navigate to your USB drive (
cd D:\ if D is your USB), and run .\Set-AutoHideTaskbar.ps1.
- Follow Prompts: The script will output its progress and ask if you want to restart Explorer for the current user.
This script is fairly comprehensive. It handles the current user, the default user template, and iterates through existing user profiles, attempting to load and modify their NTUSER.DAT files. The reg load and reg unload commands are crucial for accessing inactive user hives.