Skip to content

Instantly share code, notes, and snippets.

@mortenscheel
Last active April 5, 2025 00:10
Show Gist options
  • Save mortenscheel/81c2b0a85e5fac5b2aedc86878089ab0 to your computer and use it in GitHub Desktop.
Save mortenscheel/81c2b0a85e5fac5b2aedc86878089ab0 to your computer and use it in GitHub Desktop.
Create new local admin user in Windows 11, to avoid automatic 5-character account name with Microsoft Account.
#Requires -RunAsAdministrator
# Check if running as administrator
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Error: This script requires administrator privileges. Please run PowerShell as Administrator." -ForegroundColor Red
Exit
}
Clear-Host
Write-Host "Create Custom Admin User Account" -ForegroundColor Cyan
Write-Host "=================================" -ForegroundColor Cyan
# Get username
$Username = Read-Host "`nEnter the desired username"
# Check if username already exists as an account
try {
$userExists = Get-LocalUser -Name $Username -ErrorAction SilentlyContinue
if ($userExists) {
Write-Host "Error: A user account with the name '$Username' already exists." -ForegroundColor Red
Exit
}
} catch { }
# Check if a user folder already exists for this username
$userFolderPath = "C:\Users\$Username"
if (Test-Path -Path $userFolderPath) {
Write-Host "Error: A user folder '$userFolderPath' already exists." -ForegroundColor Red
Write-Host "Choose a different username or remove the existing folder first." -ForegroundColor Red
Exit
}
# Get full name (optional)
$FullName = Read-Host "Enter full name (optional, press Enter to use username)"
if ([string]::IsNullOrWhiteSpace($FullName)) {
$FullName = $Username
}
# Get description (optional)
$Description = Read-Host "Enter account description (optional, press Enter for default)"
if ([string]::IsNullOrWhiteSpace($Description)) {
$Description = "Admin account"
}
# Get password
$Password = Read-Host "Enter password"
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
# Show summary and confirm
Write-Host "`nAccount Summary:" -ForegroundColor Yellow
Write-Host "Username: $Username"
Write-Host "Full Name: $FullName"
Write-Host "Description: $Description"
Write-Host "Password: $Password" # Show plaintext password
Write-Host "Admin: Yes"
Write-Host "Password Never Expires: Yes"
$confirm = Read-Host "`nCreate this account? (y/n)"
if ($confirm -ne "y") {
Write-Host "Operation cancelled." -ForegroundColor Yellow
Exit
}
# Create the user
try {
New-LocalUser -Name $Username -Password $SecurePassword -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires:$true
Add-LocalGroupMember -Group "Administrators" -Member $Username
Write-Host "`nSuccess! User account '$Username' created with admin privileges." -ForegroundColor Green
Write-Host "You can now log in with this account and link it to your Microsoft account in Settings." -ForegroundColor Cyan
} catch {
Write-Host "Error creating user: $_" -ForegroundColor Red
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment