-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateUser.ps1
25 lines (17 loc) · 907 Bytes
/
CreateUser.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$userName = "NewLocalUser"
$password = "P@ssw0rd123456789!"
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$localContext = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList ([System.DirectoryServices.AccountManagement.ContextType]::Machine)
$existingUser = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($localContext, $userName)
if ($existingUser -eq $null) {
$newUser = New-Object -TypeName System.DirectoryServices.AccountManagement.UserPrincipal -ArgumentList ($localContext)
$newUser.Name = $userName
$newUser.SetPassword($password)
$newUser.UserCannotChangePassword = $true
$newUser.PasswordNeverExpires = $true
$newUser.Enabled = $true
$newUser.Save()
Write-Host "User '$userName' created successfully."
} else {
Write-Host "User '$userName' already exists."
}