forked from jSkripts/PowerShell-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisableADAccounts.ps1
66 lines (58 loc) · 1.75 KB
/
DisableADAccounts.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
<#
Created By Joel aka jskripts
https://github.com/jSkripts
#>
# Script to disable accounts from list
# Account name must be in from of SamAccountName
# TODO: Create GUI (Not needed but would be nice)
# Paramiters needed for script
Param (
[string]$InputFile = (Read-Host "File Path to Users"),
[string]$OutputFile = (Read-Host "Path to Output")
)
Import-Module ActiveDirectory -ErrorAction SilentlyContinue
$Result = @()
$DisabledCount = 0
$AlreadyDisabledCount = 0
$NotFoundCount = 0
Get-Content $InputFile | ForEach-Object {
$User = $null
$User = Get-ADUser $_
If ($User) {
If ($User.Enabled) {
$User | Set-ADUser -Enabled $false
$Result += New-Object PSObject -Property @{
User = $User.Name
DN = $User.distinguishedName
Status = "Disabled"
}
$DisabledCount ++
}
Else {
$Result += New-Object PSObject -Property @{
User = $User.Name
DN = $User.distinguishedName
Status = "Already disabled"
}
$AlreadyDisabledCount ++
}
}
Else {
$Result += New-Object PSObject -Property @{
User = $_
DN = "N/A"
Status = "User not found"
}
$NotFoundCount ++
}
}
# Show results with Out Gridview
$Result = $Result | Select-Object User,Status,DN
$Result | Out-GridView
$Result | Export-CSV $OutputFile -NoTypeInformation
Clear-Host
# Output to console
Write-Host "Accounts in file: $((Get-Content $InputFile).Count)"
Write-Host "Users Disabled: $DisabledCount"
Write-Host "Users Already Disabled: $AlreadyDisabledCount"
Write-Host "Users Not Found: $NotFoundCount"