microsoft_windows:adduser_powershell
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
microsoft_windows:adduser_powershell [2025/05/15 18:39] – created rodolico | microsoft_windows:adduser_powershell [2025/05/16 01:18] (current) – rodolico | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Add/Update User with PowerShell ====== | ====== Add/Update User with PowerShell ====== | ||
+ | ===== Discussion ===== | ||
We needed a way to automatically update a local user on a bunch of systems which were not on an Active Directory configuration. We had remote access, and the ability to run PowerShell scripts as an administrator. | We needed a way to automatically update a local user on a bunch of systems which were not on an Active Directory configuration. We had remote access, and the ability to run PowerShell scripts as an administrator. | ||
It should not be interactive at all. | It should not be interactive at all. | ||
- | The first step is to generate a password hash (what Windows callas a SecureString) so we are not passing around passwords in plaintext. | + | ===== Generate |
- | <code powershell genPass.ps> | + | The first step is to generate a password hash (what Windows calls a SecureString) so we are not passing around passwords in plaintext. |
+ | |||
+ | <code powershell genPass.ps1> | ||
$password = ConvertTo-SecureString -String " | $password = ConvertTo-SecureString -String " | ||
$plain = convertFrom-securestring -securestring $password | $plain = convertFrom-securestring -securestring $password | ||
Line 13: | Line 16: | ||
</ | </ | ||
- | The final line will give a very long hex number, which is the hash of the password (" | + | The final line will give a very long hex number, which is the hash of the password (" |
+ | |||
+ | ===== Download and Modify script ===== | ||
+ | |||
+ | Download the following Powershell file and edit in your favorite text editor. Paste the output of the previouss code into this script | ||
+ | |||
+ | Adjust | ||
+ | * $password: Replace | ||
+ | * $user: This will be the username you log in as | ||
+ | * $group: The group to add the user to | ||
+ | * $fullname: The display name of the user (optional) | ||
+ | * $description: | ||
- | <code powershell adduser.ps> | + | <code powershell adduser.ps1> |
# script to add a local user with admin privileges on a Windows machine | # script to add a local user with admin privileges on a Windows machine | ||
# Generate the password hash with the following three lines (after changing " | # Generate the password hash with the following three lines (after changing " | ||
Line 30: | Line 44: | ||
$password = ConvertTo-SecureString -String "Very Long Hex String from above" | $password = ConvertTo-SecureString -String "Very Long Hex String from above" | ||
- | |||
$user = ' | $user = ' | ||
$group = " | $group = " | ||
+ | $fullname = "Test Account" | ||
+ | $description = 'Test Account' | ||
# Check if user exists | # Check if user exists | ||
if (-not (Get-LocalUser -Name $user -ErrorAction SilentlyContinue)) { | if (-not (Get-LocalUser -Name $user -ErrorAction SilentlyContinue)) { | ||
try { | try { | ||
- | New-LocalUser -Name $user -Password $password -FullName | + | New-LocalUser -Name $user -Password $password -FullName |
Write-Host "User ' | Write-Host "User ' | ||
} catch { | } catch { | ||
Line 65: | Line 80: | ||
Write-Warning " | Write-Warning " | ||
} | } | ||
- | |||
</ | </ | ||
+ | |||
+ | ===== Run the code ===== | ||
+ | |||
+ | You can run the code by opening PowerShell as Administrator, | ||
+ | |||
+ | ===== Enhancements ===== | ||
+ | |||
+ | Note, if the password hash is to be transported over public media (e-mail, ftp, chat), you may want add the -Key or -SecureKey parameters to the encoding (ConvertTo-SecureString) and decoding (ConvertTo-SecureString) commands. | ||
+ | |||
+ | -SecureKey appearantly uses single pad encryption to further secure the key. See https:// | ||
+ | |||
+ | ===== Links ===== | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | Also, thanks to DavidN for tightening it up a little for me. |
microsoft_windows/adduser_powershell.1747352374.txt.gz · Last modified: 2025/05/15 18:39 by rodolico