check for local userif that exists, reset the password and add it to the local administrators group if it does not exist. Create and enable it,

script to check for local user "ITAdmin" if that exists, reset the password and add it to the local administrators group if it does not exist. Create and enable it, add it to the Local Administrators group and give me a report if the task is completed or failed on each client. The password is the same for all clients.

# ===== CONFIG =====
$username = "username"
$password = "P@ssw0rd"

$device = $env:COMPUTERNAME

# Convert password
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force

try {
# Check if user exists
$user = Get-LocalUser -Name $username -ErrorAction SilentlyContinue

if ($user) {
# User exists → reset password + enable
Set-LocalUser -Name $username -Password $securePassword
Enable-LocalUser -Name $username

$action = "EXISTING_USER_UPDATED"
}
else {
# User does not exist → create it
New-LocalUser -Name $username -Password $securePassword -FullName "IT Admin" -Description "TASProvider"
Enable-LocalUser -Name $username

$action = "USER_CREATED"
}

# Ensure member of Administrators group
$isMember = Get-LocalGroupMember -Group "Administrators" -Member $username -ErrorAction SilentlyContinue

if (-not $isMember) {
Add-LocalGroupMember -Group "Administrators" -Member $username
$groupAction = "ADDED_TO_ADMIN_GROUP"
}
else {
$groupAction = "ALREADY_IN_ADMIN_GROUP"
}

# Final verification
$verifyUser = Get-LocalUser -Name $username
$verifyAdmin = Get-LocalGroupMember -Group "Administrators" -Member $username -ErrorAction SilentlyContinue

if ($verifyUser -and $verifyAdmin) {
Write-Output "$device,SUCCESS,$action,$groupAction"
}
else {
Write-Output "$device,FAILED,VERIFICATION_FAILED"
}

}
catch {
Write-Output "$device,FAILED,$($_.Exception.Message)"
}

  • powershell, script, Local Username
  • 0 Els usuaris han Trobat Això Útil
Ha estat útil la resposta?

Articles Relacionats

Get the free space and total size of each drive in gigabytes using PowerShell

To accurately get the free space and total size of each drive in gigabytes using PowerShell, you...

Script to Calculate Folder Sizes PowerShell

$volumePath = "C:\" # Replace this with the path of the volume root you want to analyze # Get...

.ps1 cannot be loaded because the execution of scripts is disabled on this system

  We need to run the following command: Set-ExecutionPolicy -scope CurrentUser Unrestricted