All Active Directory Users ListBox from PowerShell

Hi All,

I am attempting to put together a PowerShell script for the creation of distribution lists as part of all other Active Directory related tasks i have already put together successfully (create a user account, resource, disable account etc).

My PowerShell script to create a new DL is sorted but what i would like to do is display a Windows Form (Vb.NET) which will query a specific OU in Active Directory for all user accounts and display them in a listbox or similar so that the user can select (multiple at once if possible) users from the listbox and then click the OK button to return to PowerShell and have PowerShell add the selected users as members of the new distribution list that was created.

I have successfully generated a Windows form from my PowerShell script with an OK and Cancel button as well as a listbox etc but the querying of Active Directory>Displaying in ListBox and selecting from Listbox if possible i have no idea how to do.

Thanks in advance


You’re going to have to stop mixing and matching vb with powershell. Look what’s possible all within PS.

I should have re-phrased, i am using the System.Windows.Forms method from PowerShell which has generated the form, i just need to query all user accounts from AD into a List box within this form.

Thanks for the link Dan, will check it out.

Before we even get to filling the dropdown, some other questions\considerations:

  • Where will this utility be running from? You can't simply use Get-ADUser or Add-ADGroupMember (module ActiveDirectory) because that is only available for systems with Remote Server Administration Tools installed and the Active Directory Powershell Module installed\available. You'll need to use DirectorySearcher or a method of searching that has no dependencies on the end points that will be launching the application.
  • What do you want the users to see versus what your program needs. Typically, you want to show the user the DisplayName and your program is going to use the SamAccountName or DN to do an action like add to a group. Typically, a listbox has a Description (DisplayName) and Value (Whatever your program needs).
  • It is assumed the user will not have permissions to actually add users to the group, so make sure you function to add a user to a group has credentials sufficient to perform the add.
  • How is you creating this form? Are you using Sapien Powershell Studio or some other WYSIWYG interface to build the form? Sapien has some 'helper' functions to load listboxes dynamically generated, so how is the actual form being coded?
  • How many users are you populating? 100..1000..10000? Most of the places I've worked have anywhere from 30-100k+ users in Active Directory, so from a query performance to a usability standpoint, approx how many users will be in the listbox?You may need to consider a search interface versus filling a multi-select listbox with every user

Hi Rob,

The PowerShell script is running on my work environment with RSAT installed, Exchange Server 2010 tools are also installed etc. As with the rest of my scripts which are completed and work 100% this new script i made starts off importing Active Directory and Exchange (add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010 and Import-Module ActiveDirectory) and then proceeds to ask the user running the scripts some questions (i.e What is the Alias to be set to, the display name etc) using Read-Host method all in the normal PowerShell console. It then creates the DL successfully and mail enables it.

After the DL is created the PowerShell script then generates a form using the System.Windows.Forms method and currently it asks the user to enter the usernames of the users to be added to the DL in a multiline textbox. The user then clicks ok and are returned to the PowerShell console and the contents of the textbox are placed into an array with the Split method. I then stopped here as this was not how i was envisaging the final result.

All users running the script have permissions to create user accounts, DLs and use the Exchange Server management tools to create mailboxes erc so permissions are not an issue.

From last count there are approximately 7800 user accounts on our network.

How i envision it is to somehow have the Win form that is generated from the PowerShell script display a listbox and then populate it with the 7800 accounts so that the user can scroll through to select the multiple users required to be members of the new DL that was just created. They would then click the OK button and would be returned to the PowerShell console where the script would then add the selected members to the new DL via Add-ADGroupMember or similar. The PowerShell script uses the Write-Progress method to show progress in the console

I know all this doesn’t sound like a tidy way to do things but its just i have invested alot of time into using the PowerShell script from the console so would like to finish off this last script to be done with it.

By the way i am not using Sapien, its just straight from PowerShell ISE and using the .NET framework.

Hope this makes sense.


Now you’re doing powershell mashups:D All of this can be done within the form.

See the link above, replace the datagridview with a listview. Add progressbars, statusbars, buttons and whatever else.

Here’s another one.