Technology Solutions for Everyday Folks
Frantically searching through report data

Powershell to Find and Export AD Records

With the very near end of life for Windows 7, as we work through the last bit of known and managed machines to upgrade or replace I find myself needing to do more frequent "searches" of AD computer object records for analysis.

Given the nature of how we're set up in the enterprise with AD and MEMCM (SCCM), combined with the fact that we're still in mid-semester/winter break, it's been wise to use both environments for source data. This data is then imported into various tools and processed for action steps (tech dispatch, direct email to end users, etc.). I'm skipping out on how we obtain MEMCM data because it's less straightforward due to how and what we're analyzing for the project.

How Did I Do This, Again?

I've been using a simple script to pull fleet-wide data for some time, especially when it's spanning across AD OU's or needs to be exported/imported into something else.

But, the script I wrote a long time ago was always stored in some random location that seemed obvious at the time. And it's never obvious when I need it. (more frequent as of late). So I finally created a GitHub Gist so I don't have to go searching across multiple servers or shares to find the damn thing next time.

Enter Powershell

The script I have is pretty simple:

Get-ADComputer -Filter 'Name -like "MNS*"' -Property * | 
Select-Object Name, Description, DistinguishedName, OperatingSystem, OperatingSystemVersion,
@{n='LastLogonTimeStamp';e={[DateTime]::FromFileTime($_.LastLogonTimeStamp)}}, WhenCreated, IPv4Address | 
Export-CSV .\WorkstationsLikeMNS.csv -NoTypeInformation -Encoding UTF8

Search and return the properties for all AD computer records starting with MNS. Then get me just a few of the properties that I need for the particular job at hand. Finally export this bad boy to a file called WorkstationsLikeMNS.csv for action.

There are better ways to deal with -Property * | Select-Object, but this has given me the most flexibility without having to remember all of the properties available, especially when I need to add or remove a specific field from the result set. If this were running on a production system (not ad-hoc) on some schedule, you'd want to parse this out a bit better.

Remember, Script the Little Things

Once upon a time (just a few years ago), I used to do this in a very manual way and stitch stuff back together as necessary (read: GUI export and Excel). As I started doing this more often, it reached my pain threshold for silly work and I took a few minutes to Google it and make Future Me more efficient.

Headline image via giphy