Created
February 22, 2024 09:07
-
-
Save Vladkarok/4e2db2bc61bcce444152fb1368aa3deb to your computer and use it in GitHub Desktop.
Powershell script to add local users using additional csv file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# USAGE: | |
# .\createuseers.ps1 -File users.csv | |
# users.csv containing: | |
# Username,Password,FullName,Description | |
# user1,password1,Full Name 1,Description 1 | |
# user2,password2,Full Name 2,Description 2 | |
# ... | |
# Import the CSV file as an array of objects | |
param([string]$File) | |
$Users = Import-Csv $File | |
# Loop through each user in the array | |
foreach ($User in $Users) { | |
# Display the username and full name (if provided) | |
$Username = $User.Username | |
$Password = ConvertTo-SecureString $User.Password -AsPlainText -Force | |
$FullName = $User.FullName | |
$Description = $User.Description | |
# Check if the user already exists | |
if (Get-LocalUser -Name $Username -ErrorAction SilentlyContinue) { | |
Write-Host "User $Username already exists. Skipping..." | |
continue | |
} | |
# Display what the script is about to do | |
Write-Host "Creating user $Username with password, full name: $FullName, and description: $Description" | |
# Create the user account | |
$NewUser = New-LocalUser -Name $Username -Password $Password | |
$NewUser | Set-LocalUser -PasswordNeverExpires $true | |
$NewUser | Set-LocalUser -FullName $FullName | |
$NewUser | Set-LocalUser -Description $Description | |
# Add the user to the "Users" and "Remote Desktop Users" groups | |
Add-LocalGroupMember -Group "Users" -Member $Username | |
Add-LocalGroupMember -Group "Remote Desktop Users" -Member $Username | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment