-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeADUserPassword.ps1
More file actions
49 lines (41 loc) · 1.52 KB
/
ChangeADUserPassword.ps1
File metadata and controls
49 lines (41 loc) · 1.52 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
Try{
#Load AD Module
$DC = New-PSSession -ComputerName DC
Import-Module -PSsession $DC -Name ActiveDirectory
}
Catch{
#AD Module failed
Write-Host "Loading AD Module failed!" -ForegroundColor Red
$PSitem
break
}
#Get users sAMAccountName
$user = Read-Host -Prompt 'Input AD-Username'
#Or get current user sAMAccountName
#$user = $env:UserName
do {
#Type in password
$newPassword = Read-Host -Prompt 'Input new password' -AsSecureString
$ReTypeNewPassword = Read-Host -Prompt 'Retype new password' -AsSecureString
#Encrypt password for comparing
$newPassword_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($newPassword))
$ReTypeNewPassword_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($ReTypeNewPassword))
#Compare password if match
if($newPassword_text -ne $ReTypeNewPassword_text){
Write-Host "Password does not match! Please retype" -ForegroundColor Red
}
else {
$newPassword = ConvertTo-SecureString $newPassword -AsPlainText -Force
}
}while($newPassword_text -ne $ReTypeNewPassword_text)
Try{
#Proceed PW Change at AD
Set-ADAccountPassword -Identity $user -NewPassword $newPassword -Reset
Write-Host "Password changed!" -ForegroundColor Green
}
Catch{
#Changing password failed
Write-Host "An error occurred" -ForegroundColor Red
$PSitem
break
}