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:56] – rodolico | microsoft_windows:adduser_powershell [2025/05/25 01:24] (current) – rodolico | ||
|---|---|---|---|
| Line 113: | Line 113: | ||
| $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 $fullName -Description $description | + | |
| + | | ||
| + | } 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 correct group | # Ensure user is in correct group | ||
| if (-not (Get-LocalGroupMember -Group $localGroup -Member $userName -ErrorAction SilentlyContinue)) { | if (-not (Get-LocalGroupMember -Group $localGroup -Member $userName -ErrorAction SilentlyContinue)) { | ||
| - | Add-LocalGroupMember -Group $localGroup -Member $userName | + | |
| + | | ||
| + | } catch { | ||
| + | Write-Error " | ||
| + | exit 1 | ||
| + | } | ||
| } | } | ||
| + | # Output success message | ||
| + | Write-Host "User ' | ||
| </ | </ | ||
| Line 136: | 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 150: | 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.1748148976.txt.gz · Last modified: 2025/05/24 23:56 by rodolico
