-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalUser.ps1
More file actions
83 lines (73 loc) · 3.36 KB
/
localUser.ps1
File metadata and controls
83 lines (73 loc) · 3.36 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
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
83
[bool] $ToRunApp = $true
while($ToRunApp){
[CmdletBinding()]
[string]$Command = Read-Host -Prompt "Enter one of the following commands: Create, Remove, Rename, Quit"
[string[]]$ComputerName = $env:COMPUTERNAME
# Creating the user
if($Command -eq "Create") {
[string]$ObjectName = Read-Host -Prompt "Enter a username for the user account"
$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) {
if(Test-Connection -ComputerName $Computer -count 1 -Quiet) {
try {
$CompObject = [ADSI]"WinNT://$Computer"
$NewObj = $CompObject.Create("User",$ObjectName)
$NewObj.SetPassword($PlainPassword)
$NewObj.SetInfo()
Write-Host "User account with the name $ObjectName created successfully" -ForegroundColor Green
} catch {
Write-Warning "Error occurred while creating the user"
Write-Verbose "More details : $_"
}
} else {
Write-Warning "$Computer is not online or avaliable"
}
}
}
# Removing the user
elseif($Command -eq "Remove") {
[string]$ObjectName = Read-Host -Prompt "Enter the username for the user account to be removed"
foreach($Computer in $ComputerName) {
if(Test-Connection -ComputerName $Computer -count 1 -Quiet) {
try {
Remove-LocalUser -Name $ObjectName
Write-Host "Removed the user account: $ObjectName" -ForegroundColor Green
} catch {
Write-Warning "Error occurred while removing the user"
Write-Verbose "More details : $_"
}
} else {
Write-Warning "$Computer is not online or avaliable"
}
}
}
# Renaming the user
elseif($Command -eq "Rename") {
[string]$CurrentUsername = Read-Host -Prompt "Enter the username for the user account to be renamed"
[string]$NewUsername = Read-Host -Prompt "Enter the new username"
foreach($Computer in $ComputerName) {
if(Test-Connection -ComputerName $Computer -count 1 -Quiet) {
try {
Rename-LocalUser -Name $CurrentUsername -NewName $NewUsername
Write-Host "Renamed the user account $CurrentUsername to $NewUsername" -ForegroundColor Green
} catch {
Write-Warning "Error occurred while renaming the user"
Write-Verbose "More details : $_"
}
} else {
Write-Warning "$Computer is not online or avaliable"
}
}
}
# Quitting the application
elseif($Command -eq "Quit") {
$ToRunProgram = $false
Write-Host "Quitting application..." -ForegroundColor Yellow
}
# Error Unrecognized Command
else {
Write-Host "Command input not recognized, please try again." -ForegroundColor Red
}
}