Select Page

In this guide, we will how to setup a local administrator in windows 10 using PowerShell.

  1. Open Start on Window 10
  2. Search for Powershell ISE, right-click the top result and select Run as Administrator
  3. Copy and paste the code below into thePowerShell ISE editor and save as createAdmin.ps1

Edit the $Username=”New_Account_name” to the username you want. Edit the $Password to the Password you want it to be.

#Requires -RunAsAdministrator
#Variables
$Username = "testuser"
$Password = "password123!"
$group = "Administrators"

$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
$existing = $adsi.Children | Where-Object { $_.SchemaClassName -eq 'user' -and $_.Name -eq $Username }

    Write-host "Calling local admin script"  -ForegroundColor Green

    if ($null -eq $existing) {
        Write-Host "Creating new local user $Username."
        & NET USER $Username $Password /add /y /expires:never
    
        Write-Host "Adding local user $Username to $group."
        & NET LOCALGROUP $group $Username /add
    }
    else {
        Write-Host "Setting password for existing local user $Username."
        $existing.SetPassword($Password)
    }

    Write-Host "Ensuring password for $Username never expires."

Note: Keeping a password stored in Script would be considered a security risk, Consider replacing the $Password variable with code snippet below

$Password = Read-Host 'What is your password?' -AsSecureString