Powershell script to add administrator to Windows server

A simple script to create and add an administrator to a Windows server.

  • Not setup to work remotely.
  • Script asks for the username. Then creates the user, creates a password, adds the user to the administrators group and writes the username, password, server name, and IP address to the powershell window.
  • I RDP into a server. Named my script, then copy it to the desktop. After that just open powershell as admin and use this:
cd $env:USERPROFILE\Desktop
.\AddAdministratorUser.ps1

 

Script *Update 12/20/16 РAdded a function I found online to create a more friendly password:


# Generate a friendly random password
# Usage: random-password <length>
Function random-password ($length = 15)
{
$punc = 46..46
$digits = 48..57
$letters = 65..90 + 97..122

$password = get-random -count $length `
-input ($punc + $digits + $letters) |
% -begin { $aa = $null } `
-process {$aa += [char]$_} `
-end {$aa}

return $password
}
$user = Read-Host -Prompt 'Input your username here'

Add-Type -Assembly System.Web
#uncomment and comment out $pass for less friendly password
#$pass=[Web.Security.Membership]::GeneratePassword(14,0)
$pass = random-password
NET USER $user "$pass" /ADD /y
NET LOCALGROUP "Administrators" "$user" /add
WMIC USERACCOUNT WHERE "Name='$user'" SET PasswordExpires=FALSE

Write-Host "Username: $user" -backgroundcolor black
Write-Host "Password: $pass" -foregroundcolor red -backgroundcolor black
Write-host "Server: $env:computername.westus.cloudapp.azure.com" -backgroundcolor black
Write-host IPaddress: (Get-NetIPAddress | where {$_.AddressFamily -eq "IPv4" -and $_.IPAddress -ne "127.0.0.1"} | select IPAddress).IPAddress -backgroundcolor black

Leave a Comment

Your email address will not be published.