Search code examples
powershellwinformsmessagebox

Trying NOT to use Out-GridView


I use a call to GetADUser, which is just returning the Sam Account Name of all the users in an OU.

I'm trying to display them in a System Windows Forms Message Box but its not working.

I know I'm close, but I'm having a brain fart. Is there a better way of doing this? Again, I'm trying very hard to avoid using Out-GridView. I'd love to have something like this:

UserTest1
UserTest2
UserTest3

I tried a couple of google searches but they were not really what I'm trying to do. Any help would be appreciated Thank you


Solution

  • If you really want to use a WinForms message box for this:

    Add-Type -AssemblyName System.Windows.Forms
    
    # Create 20 sample user names.
    # In your real code, use:
    #   $users = (Get-ADUser -filter * -Searchbase $OU).SamAccountName
    $users = 1..20 | ForEach-Object { "user" + $_ }
    
    # Display a message box with each username on its own line.
    [Windows.Forms.MessageBox]::Show(
      $users -join "`n",
      'Title',       # window title
      'OKCancel',    # buttons
      'Information'  # icon
    )
    

    Note that this is only suitable for a limited number of usernames, namely as many as will fit on your screen at once, excluding the space needed to display the title bar and the button(s).
    If there are too many, not all will be visible, and the button(s) won't show.

    To avoid this problem, you'd need to create a custom form with a (scrollable) list box.