-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportADUsers.ps1
More file actions
61 lines (45 loc) · 1.85 KB
/
ExportADUsers.ps1
File metadata and controls
61 lines (45 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
50
51
52
53
54
55
56
57
58
59
60
61
#Path
$Path = Split-Path -Parent "C:\Scripts\*.*"
#Check if path exist
If(!(test-path $Path))
{
New-Item -ItemType Directory -Force -Path $Path
}
#Create variable for the date stamp
$timeStamp = Get-Date -f yyyy-MM-dd-hh-mm
#Define CSV
$CSVfile = $Path + "\ADUsers_$timeStamp.csv"
#Import Active Directory module
Import-Module ActiveDirectory
#Set distinguishedName as Array-List
$DNs = New-Object System.Collections.Generic.List[System.Object]
do {
$distinguishedName = Read-Host -Prompt 'Input distinguishedName'
$DNs.Add($distinguishedName)
$continue = Read-Host -Prompt 'Do you want to put more distinguishedNames (Y/N)?'
} while ($continue -eq 'Y' -or $continue -eq 'y' -or $continue -eq '')
#Create empty array
$AllADUsers = @()
Write-Host 'Connecting to AD...'
#Loop thru every DN
foreach ($DN in $DNs) {
$Users = Get-ADUser -SearchBase $DN -Filter * -Properties *
#Add users to array
$AllADUsers += $Users
}
#Create list
$AllADUsers | Sort-Object Name | Select-Object `
@{Label = "First name"; Expression = { $_.GivenName } },
@{Label = "Last name"; Expression = { $_.Surname } },
@{Label = "User logon name"; Expression = { $_.SamAccountName } },
@{Label = "User principal name"; Expression = { $_.UserPrincipalName } },
@{Label = "Country/region"; Expression = { $_.Country } },
@{Label = "Job Title"; Expression = { $_.Title } },
@{Label = "Telephone number"; Expression = { $_.telephoneNumber } },
@{Label = "E-mail"; Expression = { $_.Mail } },
@{Label = "Account status"; Expression = { if (($_.Enabled -eq 'TRUE') ) { 'Enabled' } Else { 'Disabled' } } },
@{Label = "Last logon date"; Expression = { $_.lastlogondate } }|
#Export to CSV file
Export-Csv -Encoding UTF8 -Path $CSVfile -NoTypeInformation #-Delimiter ";"
Write-Host 'Done!'
Write-Host 'CSV File saved at' $Path