-
Notifications
You must be signed in to change notification settings - Fork 1
/
addUser.ps1
82 lines (72 loc) · 1.39 KB
/
addUser.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Setup a local user
#requires -version 4.0
#requires -runasadministrator
# Set script params
param([switch]$install, [switch]$uninstall)
<#
# Global Vars
#>
$localAcc = "User1"
$localAccPass = "P@ssW0rd"
$localAccGroup = "Administrators"
$localAccDescript = "PowerShell Created User"
<#
# Functions
#>
Function addUser($userName, $pass, $descript)
{
Write-Host("Adding Local User: $userName")
try
{
$compName = $env:COMPUTERNAME
$cn = [ADSI]"WinNT://$compName"
$user = $cn.Create('User',$userName)
$user.SetPassword($pass)
$user.setinfo()
$user.description = $descript
$user.SetInfo()
}
catch
{
$_
}
}
Function addToGroup($userName, $userGroup)
{
Write-Host("Adding $userName to $userGroup...")
try
{
$group = [ADSI]"WinNT://$env:COMPUTERNAME/$userGroup,group"
$group.Add("WinNT://$env:COMPUTERNAME/$userName,user")
}
catch
{
$_
}
}
Function rmUser($userName)
{
Write-Host("Removing Local User...")
try
{
$compName = $env:COMPUTERNAME
$cn = [ADSI]"WinNT://$compName"
$user = $cn.Delete("User", $userName)
}
catch
{
$_
}
}
<#
# Main
#>
if ($install)
{
addUser $localAcc $localAccPass $localAccDescript
addToGroup $localAcc $localAccGroup
}
elseif ($uninstall)
{
rmUser $localAcc
}