-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-Enabled Users from a Specific domains.ps1
More file actions
49 lines (42 loc) · 1.85 KB
/
Get-Enabled Users from a Specific domains.ps1
File metadata and controls
49 lines (42 loc) · 1.85 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
# Load GUI assembly
Add-Type -AssemblyName Microsoft.VisualBasic
# 1. Prompt for the Target Domain
$TargetDomain = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the FQDN of the domain you want to search (e.g. child.domain.com)", "Target Domain", "")
if ([string]::IsNullOrWhiteSpace($TargetDomain)) {
Write-Warning "No domain entered. Exiting."
exit
}
try {
Write-Host "Connecting to $TargetDomain..." -ForegroundColor Cyan
# 2. Query only Enabled users from the specific server/domain
# We use -Properties * to ensure we get everything you might need
$Users = Get-ADUser -Filter 'Enabled -eq $true' -Server $TargetDomain -Properties `
DisplayName, EmailAddress, Title, Department, Company, LastLogonDate, whenCreated
if ($null -eq $Users) {
Write-Warning "No enabled users found in $TargetDomain."
} else {
# 3. Format the results
$Results = $Users | Select-Object `
@{Name="Domain"; Expression={$TargetDomain}},
Name,
DisplayName,
SamAccountName,
UserPrincipalName,
EmailAddress,
Title,
Department,
Company,
LastLogonDate,
whenCreated
# 4. Show in GridView
Write-Host "Found $($Users.Count) enabled users." -ForegroundColor Green
$Results | Out-GridView -Title "Enabled Users in $TargetDomain"
# 5. Export to Desktop
$FilePath = "$env:USERPROFILE\Desktop\Enabled_Users_$($TargetDomain).csv"
$Results | Export-Csv -Path $FilePath -NoTypeInformation -Encoding UTF8
Write-Host "Export saved to: $FilePath" -ForegroundColor Yellow
}
}
catch {
Write-Error "Could not connect to domain '$TargetDomain'. Ensure the name is correct and you have network connectivity."
}