microsoft_windows:adduser_powershell
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
microsoft_windows:adduser_powershell [2025/05/24 23:44] – rodolico | microsoft_windows:adduser_powershell [2025/05/25 01:24] (current) – rodolico | ||
---|---|---|---|
Line 55: | Line 55: | ||
* $key: Replace the comma separated integers with the 32 integers in the file aes.key | * $key: Replace the comma separated integers with the 32 integers in the file aes.key | ||
* $securePassword: | * $securePassword: | ||
+ | * $fullName: This is the diplay name of the user | ||
+ | * $description: | ||
+ | * $localGroup: | ||
+ | |||
<code powershell updateUser.ps1> | <code powershell updateUser.ps1> | ||
Line 90: | Line 94: | ||
# Note: The encrypted key will end with ' | # Note: The encrypted key will end with ' | ||
$securePassword = ' | $securePassword = ' | ||
+ | |||
+ | $fullName = "" | ||
+ | $description = "" | ||
+ | $localGroup = "" | ||
+ | |||
+ | # if $fullName is empty or null | ||
+ | if (-not $fullName) { | ||
+ | $fullName = $userName | ||
+ | } | ||
+ | |||
+ | # if $description is empty or null | ||
+ | if (-not $description) { | ||
+ | $description = "User created by script" | ||
+ | } | ||
+ | |||
+ | # if $localGroup is empty or null | ||
+ | if (-not $localGroup) { | ||
+ | $localGroup = " | ||
+ | } | ||
# Check if user exists, create if not | # Check if user exists, create if not | ||
if (-not (Get-LocalUser -Name $userName -ErrorAction SilentlyContinue)) { | if (-not (Get-LocalUser -Name $userName -ErrorAction SilentlyContinue)) { | ||
- | New-LocalUser -Name $userName -Password $securePassword -FullName | + | |
+ | | ||
+ | } catch { | ||
+ | Write-Error | ||
+ | exit 1 | ||
+ | } | ||
} | } | ||
# Set the password (update if user exists) | # Set the password (update if user exists) | ||
- | Set-LocalUser -Name $userName -Password $securePassword | + | try { |
+ | | ||
+ | } catch { | ||
+ | Write-Error " | ||
+ | exit 1 | ||
+ | } | ||
- | # Ensure user is in Administrators | + | # Ensure user is in correct |
- | if (-not (Get-LocalGroupMember -Group | + | if (-not (Get-LocalGroupMember -Group |
- | Add-LocalGroupMember -Group | + | |
+ | | ||
+ | } catch { | ||
+ | Write-Error " | ||
+ | exit 1 | ||
+ | } | ||
} | } | ||
+ | # Output success message | ||
+ | Write-Host "User ' | ||
</ | </ | ||
Line 112: | Line 152: | ||
* Do not send the script over any public media like e-mail. You can safely send the $key line, or the $securePassword line, but not both. | * Do not send the script over any public media like e-mail. You can safely send the $key line, or the $securePassword line, but not both. | ||
- | * You can easily change the group to add to. I'd suggest replacing Administrators (two instances near bottom) with a variable, then define the variable at the top. | + | * Multiple groups could be set up by changing group to an array and then looping through them. |
- | * FullName and Description likewise could be set up in variables if this script will be used multiple times | + | * <del>You can easily change the group to add to. I'd suggest replacing Administrators (two instances near bottom) with a variable, then define the variable at the top.</ |
+ | * <del>FullName and Description likewise could be set up in variables if this script will be used multiple times</ | ||
===== Links ===== | ===== Links ===== | ||
Line 126: | Line 167: | ||
* https:// | * https:// | ||
* https:// | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
Also, thanks to DavidN for tightening it up a little for me. | Also, thanks to DavidN for tightening it up a little for me. |
microsoft_windows/adduser_powershell.1748148241.txt.gz · Last modified: 2025/05/24 23:44 by rodolico