-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADUsersToCSV.ps1
More file actions
37 lines (27 loc) · 1.04 KB
/
Copy pathADUsersToCSV.ps1
File metadata and controls
37 lines (27 loc) · 1.04 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
# Run this script from a domain controller
Import-Module ActiveDirectory
$SearchBase = ""
$File = "ADUsers.csv"
$FileTemp = "temp.csv"
# Get user input
$DomainName = Read-Host "Enter the domain name"
$OU = Read-Host "Enter the organization unit"
# Split the string by .
$DomainParts = $DomainName.split(".")
# Create the LDAP search string
$SearchBase = "OU=" + $OU + ","
ForEach($Part in $DomainParts) {
$SearchBase = $SearchBase + "DC=" + $Part + ","
}
# Remove the last character (the extra comma)
$SearchBase = $SearchBase.Substring(0,$SearchBase.Length-1)
Write-Host "Searching: " $SearchBase
# Return a list of users
$Users = Get-ADUser -Filter * -SearchBase $SearchBase -Properties Office, EmailAddress, Title, OfficePhone | Where-Object {$_.Enabled -eq $True} | Select-Object SAMAccountName, Office, GivenName, Surname, EmailAddress, Title, OfficePhone
# Export data to CSV file
$Users | Export-Csv $File
# Remove the first line of the file
Get-Content $File |
Select -Skip 1 |
Set-Content "$FileTemp"
Move "$FileTemp" $File -Force