One command at a time#
One of the things I've been learning about PowerShell is how much easier it can make everyday IT tasks. Instead of manually going through a list of users to find the information I need, I can use a few commands to do the work for me.
For this lab, I was given a request from the Director of IT at Larkspur Logistics: put together a list of everyone in the IT department whose account is active, along with their name, sign-in name, and job title.
It sounds simple enough, but this was a good opportunity to practice working with CSV files, filtering information, and combining multiple PowerShell commands to get the results I wanted.
The ticket#
From: Daniel Reyes, Director of IT, Larkspur Logistics (a fictional company)
Subject: 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 was checked#
IAM Labs ran the script in its PowerShell sandbox and checked the result:
- The script completed successfully.
- The file it-staff.csv was created.
- The report matched the requirements in the ticket.
The script#
# The lab's files are in the current folder.
# 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 -NoTypeInformationMy approach#
I started by looking at the directory export I was given.
To bring that information into PowerShell, I used Import-Csv. This imported the contents of the CSV file and converted each row into a PowerShell object that I could work with.
Next, I needed to narrow down the list. I used Where-Object to filter out anyone who wasn't in the IT department or whose account wasn't enabled.
Once I had the users I needed, I used Select-Object to choose the three columns requested in the ticket: DisplayName, UserPrincipalName, and JobTitle.
Finally, I used Export-Csv to save the results into a new CSV file called it-staff.csv.
One thing I had to get comfortable with was using the pipeline (|). It allows me to take the output from one command and pass it directly into the next. Once I started understanding how that worked, combining commands became much easier.
What I learned#
This lab helped me better understand how useful PowerShell can be for working with user information. I got more comfortable using the pipeline to combine commands and filter data based on specific properties, rather than manually going through a CSV file.
It's a simple task, but one that could easily come up in a real IT environment. Being able to put together a quick report like this is a useful skill to have.
IAM Labs' record of this lab: view the full lab record.