-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet_AD_User&Computers.ps1
More file actions
36 lines (29 loc) · 1.91 KB
/
Get_AD_User&Computers.ps1
File metadata and controls
36 lines (29 loc) · 1.91 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
# Get_AD_User&Computers.ps1
# ---------------------------------------------
# This script retrieves and displays all enabled and disabled users and computers
# in Active Directory, along with their Organizational Units (OUs).
# ---------------------------------------------
Import-Module ActiveDirectory
# Function to extract OU from DistinguishedName
function Get-OU {
param ([string]$DistinguishedName)
# Remove the "CN=" and return only the OU part
return ($DistinguishedName -replace "^CN=.*?,", "")
}
# Enabled Users
$enabledUsers = Get-ADUser -Filter {Enabled -eq $true} -Properties Enabled, DistinguishedName
Write-Host "=== Enabled Users === (Count = $($enabledUsers.Count))" -ForegroundColor Green
$enabledUsers | Select-Object Name, SamAccountName, Enabled, @{Name='OU'; Expression={Get-OU $_.DistinguishedName}} | Format-Table -AutoSize
# Disabled Users
$disabledUsers = Get-ADUser -Filter {Enabled -eq $false} -Properties Enabled, DistinguishedName
Write-Host "`n=== Disabled Users === (Count = $($disabledUsers.Count))" -ForegroundColor Red
$disabledUsers | Select-Object Name, SamAccountName, Enabled, @{Name='OU'; Expression={Get-OU $_.DistinguishedName}} | Format-Table -AutoSize
# Enabled Computers
$enabledComputers = Get-ADComputer -Filter {Enabled -eq $true} -Properties Enabled, DistinguishedName
Write-Host "`n=== Enabled Computers === (Count = $($enabledComputers.Count))" -ForegroundColor Green
$enabledComputers | Select-Object Name, DNSHostName, Enabled, @{Name='OU'; Expression={Get-OU $_.DistinguishedName}} | Format-Table -AutoSize
# Disabled Computers
$disabledComputers = Get-ADComputer -Filter {Enabled -eq $false} -Properties Enabled, DistinguishedName
Write-Host "`n=== Disabled Computers === (Count = $($disabledComputers.Count))" -ForegroundColor Red
$disabledComputers | Select-Object Name, DNSHostName, Enabled, @{Name='OU'; Expression={Get-OU $_.DistinguishedName}} | Format-Table -AutoSize
pause