-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwindows_users.ps1
More file actions
42 lines (34 loc) · 1.38 KB
/
windows_users.ps1
File metadata and controls
42 lines (34 loc) · 1.38 KB
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
# https://4sysops.com/archives/powershell-script-to-create-local-user-accounts-or-local-groups/
[CmdletBinding()]
Param(
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(Mandatory=$true)]
[ValidateSet("Group","User")]
[string]$ObjectType,
[Parameter(Mandatory=$true)]
[string]$ObjectName
)
if($ObjectType -eq "User") {
$PasswordForUser = Read-Host -Prompt "Enter a password for user account" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($PasswordForUser)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
}
foreach($Computer in $ComputerName) {
Write-Host "Working on $Computer"
if(Test-Connection -ComputerName $Computer -count 1 -Quiet) {
try {
$CompObject = [ADSI]"WinNT://$Computer"
$NewObj = $CompObject.Create("$ObjectType",$ObjectName)
if($ObjectType -eq "User") {
$NewObj.SetPassword($PlainPassword)
}
$NewObj.SetInfo()
Write-Host "$ObjectTYpe with the name $ObjectName created successfully" -ForegroundColor Green
} catch {
Write-Warning "Error occurred while creating the group"
Write-Verbose "More details : $_"
}
} else {
Write-Warning "$Computer is not online"
}
}