# ------------------------------------------------------------ # Create local users from Users.csv # ------------------------------------------------------------ $CsvFile = "C:\Temp\Users.csv" $LogFile = "C:\Temp\CreateUsers.log" Start-Transcript -Path $LogFile if (-not (Test-Path $CsvFile)) { Write-Host "ERROR: CSV file not found." Stop-Transcript exit } # Resolve the localized name of the built-in Users group $UsersGroup = ([System.Security.Principal.SecurityIdentifier]"S-1-5-32-545").Translate([System.Security.Principal.NTAccount]).Value.Split("\")[1] $Created = 0 $Skipped = 0 $Errors = 0 foreach ($Entry in Import-Csv $CsvFile) { $UserName = $Entry.Name $FullName = $Entry.FullName $Password = ConvertTo-SecureString $Entry.Password -AsPlainText -Force Write-Host "" Write-Host "----------------------------------------" Write-Host "Creating user: $UserName" try { # Skip if user already exists if (Get-LocalUser -Name $UserName -ErrorAction SilentlyContinue) { Write-Host "User already exists - skipped." $Skipped++ continue } # Create the local user New-LocalUser ` -Name $UserName ` -FullName $FullName ` -Password $Password ` -AccountNeverExpires ` -PasswordNeverExpires:$false # Explicitly add the user to the built-in Users group Add-LocalGroupMember ` -Group $UsersGroup ` -Member $UserName # Force password change at next logon cmd /c "net user `"$UserName`" /logonpasswordchg:yes" | Out-Null if ($LASTEXITCODE -eq 0) { Write-Host "Password change required at next logon." } else { Write-Host "WARNING: Could not enable password change at next logon." } Write-Host "User created successfully." $Created++ } catch { Write-Host "ERROR: $($_.Exception.Message)" $Errors++ } } Write-Host "" Write-Host "========================================" Write-Host "Summary" Write-Host "========================================" Write-Host "Created : $Created" Write-Host "Skipped : $Skipped" Write-Host "Errors : $Errors" Write-Host "========================================" Stop-Transcript