Custom Intellisense for AD cmdlets with SearchBase parameter

Sup’ PSHomies!

You gotta love the PowerShell community! Found this little gem in my twitter feed (again) :-). Trevor Sullivan demonstrates how we can create custom intellisense for cmdlets if they haven’t been provided as yet. Great video! Trevor really does a great job explaining this.

The first thing that came to mind was Active Directory! I can’t tell you how often I needed the DistinguishedName of an OU. Now imagine having a dynamic list generated for you! No more errors,  just select and you’re good to go! Excited??? I sure am!

Sometimes you need to limit your searchbase depending on you AD size. Let’s say I want to retrieve all users starting from a specific point

Get-ADUser -Filter * -SearchBase 'OU=Users,OU=IT,DC=pshirwin,DC=local'

A simple typo will generate an error. Distinguished names are notorious for being lengthy…

Now the obvious AD cmdlets would be Get-ADUser,Get-ADGroup & Get-ADComputer. So that got me thinking , just how many AD cmdlets have SearchBase as a parameter?

Get-Command -Module ActiveDirectory |
   $psItem.Name |
   Where-Object {
        (Get-Command $psItem).ParameterSets.Parameters.Name -eq 'SearchBase'

Turns out there are quite a few using SearchBase

  • Get-ADComputer
  • Get-ADFineGrainedPasswordPolicy
  • Get-ADGroup
  • Get-ADObject
  • Get-ADOptionalFeature
  • Get-ADOrganizationalUnit
  • Get-ADServiceAccount
  • Get-ADUser
  • Search-ADAccount

So I can have Intellisense on all these cmdlets? Awesome!!!

Author: I.C.A. Strachan
Version History:
Purpose: Custom Intellisense completion for AD cmdlets with SearchBase parameter
ActiveDirectory & TabExpansion++ module is required.
Link to Trevor Sullivan's video demonstration:
#region Get AD cmdlets with SearchBase parameter
$ADCmdlestWithSearchBase = Get-Command Module ActiveDirectory |
$psItem.Name |
Where-Object {
(Get-Command $psItem).ParameterSets.Parameters.Name -eq 'SearchBase'
#region Configure custom intellisense for AD cmdlets with SearchBase
$sbADSearchBase= {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$ADPaths = ActiveDirectory\Get-ADOrganizationalUnit filter *
foreach ($ADPath in $ADPaths){
$completionResult =@{
CompletionText = $ADPath.DistinguishedName
ToolTip = ('The organization unit DistinguishedName {0}' -f $ADPath.DistinguishedName)
ListItemText = $ADPath.Name
CompletionResultType = 'ParameterValue'
New-CompletionResult @completionResult
$tabExpansion = @{
CommandName = $ADCmdlestWithSearchBase
ParameterName = 'SearchBase'
ScriptBlock = $sbADSearchBase
TabExpansion++\Register-ArgumentCompleter @tabExpansion

Intellisense completed the DistinguisedName on -SearchBase for me. No need to type it in, no errors, just select and go!


Here’s the result:


I’m sure you guys will find your own use for this… Thanks again Trevor for bring this to our attention! Good looking out for the community! Be sure to watch Trevor’s video for in depth explanation.

Hope it’s worth something to you…




5 thoughts on “Custom Intellisense for AD cmdlets with SearchBase parameter

  1. Pingback: Enumerations in PowerShell | pshirwin

  2. Albybara

    A one-liner for listing AD cmdlets with searchbase would be:
    (Get-Command -mod ActiveDirectory |? {$_.ParameterSets.parameters.Name -eq ‘SearchBase’}).Name



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s