Query Active Directory with PowerShell

This function queries Active Directory for users matching the specified parameter and generates a PSObject with certain properties from the returned objects. The collection of returned objects can be piped to additional PowerShell cmdlets for sorting, formatting or exporting.

The script can be called, the function placed in a profile or the script can be dot-sourced, to allow the ExportGAL function to be called like another cmdlet:
PS C:\> . .\QueryAD.ps1

The function accepts two parameters, a field and a value. These are used to build the search query. For example, the following will search for anyone with the last name of Smith:
PS C:\> QueryAD sn Smith

Building a collection of everyone in the SouthEast Marketing department could be done with this:
PS C:\> QueryAD department "SouthEast Marketing"

The collection of objects can be manipulated just like any other object:
PS C:\> QueryAD department "SouthEast Marketing" | Select-Object last,phone | Format-Table -AutoSize
PS C:\> QueryAD department "SouthEast Marketing" | ConvertTo-Html | Out-File c:\marketing.html
PS C:\> QueryAD department "SouthEast Marketing" | Export-Csv c:\marketing.csv