-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathCreate-LocalUser.ps1
43 lines (35 loc) · 1.25 KB
/
Create-LocalUser.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# http://www.selfadsi.org/ads-attributes/user-userAccountControl.htm
function Create-LocalUser {
param (
[string]$computer = $env:computername,
[string]$fullname,
[string]$username,
[string]$password,
[switch]$passworddoesnotexpire,
[string]$addtogroup = 'Administrators',
[switch]$CheckFirst = $true
)
if (!$username -or !$password) {
throw 'no username or password'
}
if ($checkfirst -and ([ADSI]"WinNT://$computer/$username").Name) {
Write-Warning "$username already exists on $computer"
return
}
$objOU = [ADSI]"WinNT://$computer"
$objUser = $objOU.Create('user', $username)
$objUser.SetPassword($password)
$objUser.SetInfo()
$objUser.FullName = $fullname
$objUser.SetInfo()
#$objUser.Description = 'Test user'
#$objUser.SetInfo()
if ($passworddoesnotexpire) {
$objUser.UserFlags = 65536 # password does not expire
$objUser.SetInfo()
}
if ($addtogroup) {
([ADSI]"WinNT://$computer/$addtogroup").Add("WinNT://$computer/$username")
}
}
# Create-LocalUser -computer $env:COMPUTERNAME -fullname 'Test' -username 'test' -password 'Password1' -passworddoesnotexpire -addtogroup 'Administrators'