Created
September 21, 2019 23:51
-
-
Save ak9999/23b12a0fef572ee34bf6b981ef4bd8cf to your computer and use it in GitHub Desktop.
Create or update a local administrator account, optionally choosing username and password.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param([string]$username='LocalAdministrator',[string]$password='P@ssw0rd!') | |
$secure_password = $password | ConvertTo-SecureString -AsPlainText -Force | |
try { | |
$LocalUserObject = Get-LocalUser -Name $username -ErrorAction Stop | |
} | |
catch [Microsoft.PowerShell.Commands.UserNotFoundException] { | |
"User $($username) Not Found" | Write-Warning | |
} | |
If (!$LocalUserObject) { | |
# splatting arguments to pass into New-LocalUser | |
$args = @{ | |
Name = $username | |
Description = "Local Administrator Account" | |
Password = $secure_password | |
} | |
New-LocalUser -UserMayNotChangePassword -AccountNeverExpires -PasswordNeverExpires @args | |
Add-LocalGroupMember -Group "Administrators" -Member $username | |
} else { | |
# splatting arguments to pass into Set-LocalUser | |
$args = @{ | |
Name = $username | |
Description = "Local Administrator Account" | |
Password = $secure_password | |
UserMayChangePassword = $false | |
} | |
Set-LocalUser -AccountNeverExpires -PasswordNeverExpires $true @args | |
Add-LocalGroupMember -Group "Administrators" -Member $username | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment