PowerShell – ActiveDirectory Searches

Cool stuff. Get every AD User Object that has an E-mail message attribute assigned:

Import-Module ActiveDirectory
Get-ADObject -Filter {(mail -like "*") -and (ObjectClass -eq "user")} -Server CORP:3268


Import-Module ActiveDirectory
Get-ADObject -Filter {(mail -like "*") -and (ObjectClass -eq "user")} -Server CORP:3268


Import-Module ActiveDirectory
Get-ADUser -Filter {(SamAccountName -like "*" -and (mailNickName -like "*")} `
| Set-ADUser -Replace @{extensionAttribute1='FoundAttribute1';extensionAttribute2='FoundAttribute2';}

Reference URLs:
http://technet.microsoft.com/en-us/library/ee617198.aspx
http://technet.microsoft.com/en-us/library/dd772723(WS.10).aspx

Extra:

Get-Command -Module *
Get-Command -Module CimCmdlets
Get-Command -Module ActiveDirectory

PowerShell – Various Annoyances

Terminating errors vs. Non-terminating errors.
This will “fail” (a.k.a. worked as designed) and skip over the catch block because it is a non-terminating error.

try
{
Import-Module BogusModule
}
catch
{
Write-Warning "BogusModule could not be loaded."
Write-Output $Error[0]
}

This will work:

try
{
Import-Module BogusModule -ErrorAction 'Stop'
}
catch
{
Write-Warning "BogusModule could not be loaded."
Write-Output $Error[0]
}

Reference function:
http://stackoverflow.com/questions/10498433/how-to-correctly-ignore-import-module-errors-in-powershell

Annoyance:
PowerShell deciding how it will format (“rolling” and “unrolling” objects) an item on output.
An intermediary function for CSV as an example:
http://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/