User Tools

Site Tools


microsoft_windows:adduser_powershell

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
microsoft_windows:adduser_powershell [2025/05/24 23:56] rodolicomicrosoft_windows:adduser_powershell [2025/05/25 01:24] (current) rodolico
Line 113: Line 113:
     $localGroup = "Users" # Default group if not specified     $localGroup = "Users" # Default group if not specified
 } }
- 
  
 # 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+    try { 
 +        New-LocalUser -Name $userName -Password $securePassword -FullName $fullName -Description $description -ErrorAction Stop 
 +    } catch { 
 +        Write-Error "Failed to create user '$userName': $_" 
 +        exit 1 
 +    }
 } }
  
 # Set the password (update if user exists) # Set the password (update if user exists)
-Set-LocalUser -Name $userName -Password $securePassword+try { 
 +    Set-LocalUser -Name $userName -Password $securePassword 
 +} catch { 
 +    Write-Error "Failed to set password for user '$userName': $_" 
 +    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+    try { 
 +        Add-LocalGroupMember -Group $localGroup -Member $userName -ErrorAction Stop 
 +    } catch { 
 +        Write-Error "Failed to add user '$userName' to group '$localGroup': $_" 
 +        exit 1 
 +    }
 } }
 +# Output success message
 +Write-Host "User '$userName' has been created or updated successfully with the specified password." -ForegroundColor Green
 </code> </code>
  
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> 
 +  * <del>FullName and Description likewise could be set up in variables if this script will be used multiple times</del>
  
 ===== Links ===== ===== Links =====
Line 150: Line 167:
   * https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/get-localuser?view=powershell-5.1   * https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/get-localuser?view=powershell-5.1
   * https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/convertto-securestring   * https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/convertto-securestring
 +  * https://community.spiceworks.com/t/use-powershell-securestring-with-windows-system-account/974434
 +  * https://stackoverflow.com/questions/7109958/saving-credentials-for-reuse-by-powershell-and-error-convertto-securestring-ke
  
 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