My question is about code efficiency. I recently had to write a Powershell 3.0 script which had the following criteria:
- It had to pull all enabled user objects from active directory
- It had to pull specific properties about each user object
- It had to display what it was doing on screen so it didn't look like it was hanging
- It had to export the results to a CSV file
I accomplished that goal after playing around with things for a while, and was happy with the result.
The thing is, whenever my more seasoned colleagues write a script, it takes a lot less time and is generally a much shorter script. While my script works, I would love some pointers and advice about how to accomplish the same thing in less code.
(Side note: I know I have cheated with the the variables for "phonetic" name stuff. I couldn't find the true properties in the time I had).
Import-Module ActiveDirectory
$userCSV = "C:\temp\users.csv" #Set output file path
$UserArray = @() #Set Array to hold information
$Domain = (Get-ADDomain).DistinguishedName #Define Domain details
$UserProperties = Get-ADUser -filter * -SearchBase $Domain #Grab a load of user object information from AD
$Users = $UserProperties.name #Filter information into a list of names
#Loop through each name in the generated list
foreach ($User in $Users) {
#Lookup each enabled user object in the list and grab all properties
$userCurrent = Get-ADUser -filter {name -like $user -and Enabled -eq $true} -Properties * | select *
#Of all the properties gathered, grab some specific ones
$Name = $userCurrent.Name
$Email = $userCurrent.EmailAddress
$FirstName = $userCurrent.GivenName
$LastName = $userCurrent.Surname
$Department = $userCurrent.Department
$DisplayName = $userCurrent.DisplayName
$Office = $userCurrent.Office
$PhoneticCompanyName = $userCurrent.Company
$PhoneticDisplayName = $userCurrent.DisplayName
$PhoneticFirstName = $userCurrent.GivenName
$PhoneticLastName = $userCurrent.Surname
$PhoneticDepartment = $userCurrent.Department
#Output process on screen
Write-Host $Name -foregroundcolor "green" -backgroundcolor "black"
Write-Host $Email -foregroundcolor "red" -backgroundcolor "black"
Write-Host $FirstName -foregroundcolor "red" -backgroundcolor "black"
Write-Host $LastName -foregroundcolor "red" -backgroundcolor "black"
Write-Host $Department -foregroundcolor "red" -backgroundcolor "black"
Write-Host $DisplayName -foregroundcolor "red" -backgroundcolor "black"
Write-Host $Office -foregroundcolor "red" -backgroundcolor "black"
Write-Host $PhoneticCompanyName -foregroundcolor "red" -backgroundcolor "black"
Write-Host $PhoneticDisplayName -foregroundcolor "red" -backgroundcolor "black"
Write-Host $PhoneticFirstName -foregroundcolor "red" -backgroundcolor "black"
Write-Host $PhoneticLastName -foregroundcolor "red" -backgroundcolor "black"
Write-Host $PhoneticDepartment -foregroundcolor "red" -backgroundcolor "black"
#Build CSV from Array of results
$UserData = New-Object PSObject
$UserData | Add-Member -type NoteProperty -name "Name" -value $Name
$UserData | Add-Member -type NoteProperty -name "E-Mail Address" -value $Email
$UserData | Add-Member -type NoteProperty -name "First Name" -value $FirstName
$UserData | Add-Member -type NoteProperty -name "Last Name" -value $LastName
$UserData | Add-Member -type NoteProperty -name "Department" -value $Department
$UserData | Add-Member -type NoteProperty -name "Display Name" -value $DisplayName
$UserData | Add-Member -type NoteProperty -name "Office" -value $Office
$UserData | Add-Member -type NoteProperty -name "Phonetic Company Name" -value $PhoneticCompanyName
$UserData | Add-Member -type NoteProperty -name "Phonetic Display Name" -value $PhoneticDisplayName
$UserData | Add-Member -type NoteProperty -name "Phonetic First Name" -value $PhoneticFirstName
$UserData | Add-Member -type NoteProperty -name "Phonetic Last Name" -value $PhoneticLastName
$UserData | Add-Member -type NoteProperty -name "Phonetic Department" -value $PhoneticDepartment
$UserArray += $UserData
}
#Do some sorting - not sure if this step is required due to object names likely being unique anyway
$UserArray = $UserArray | sort-object -Property {$_.name } -Unique
#Write CSV in UTF8 in case of foreign names
$UserArray | Export-Csv -Encoding UTF8 -LiteralPath $userCSV -NoTypeInformation
Write-Host "Total Number of Unique Users found:"$UserArray.Length #Count