IAM Labs· Lab record
Passed
PowerShell 101: filter a directory export
Filter a directory export down to IT's active staff.
The ticket
Daniel Reyes
Director of IT, Larkspur Logistics (fictional)
Who's on my team?
I need a quick list of everyone in the IT department whose account is active, with their:
- name
- sign-in name
- job title
The directory export is attached.
What IAM Labs verified
IAM Labs ran the script below in its PowerShell sandbox, on the lab's data files, and checked the result:
- ✓The script ran without errors
- ✓It wrote it-staff.csv
- ✓The report matches what the ticket asked for
Skills
Import-CsvWhere-ObjectSelect-ObjectExport-Csv
The script
The latest version that passed in the sandbox, on .
# Import the directory.csv file.
Import-Csv ./directory.csv |
# Keep only active accounts in the IT department.
Where-Object { $_.Department -eq 'IT' -and $_.AccountEnabled -eq 'True' } |
# Select the columns we want to include in the output.
Select-Object DisplayName, UserPrincipalName, JobTitle |
# Export the results to a new CSV file without an extra type information header.
Export-Csv ./it-staff.csv -NoTypeInformation