What if you did not use ListBOX but used ListView ? Listview is capable of having Columns ($objListView.View = 1 #can not remember what the different views are..) and with some more code you can enable Column sorting...
$FormWidth = 300 # 300 $FormHeight = 500 # 320 $objForm = New-Object System.Windows.Forms.Form $objForm.Text = "Select user(s)" $objForm.Size = New-Object System.Drawing.Size($FormWidth,$FormHeight) $objForm.StartPosition = "CenterScreen" ## ----- Make the ListBox ----- $objListView = New-Object System.Windows.Forms.ListView $ListViewLocationWidth = $FormWidth * 0.03 $ListViewLocationHeight = $FormHeight * 0.09 $objListView.Location = New-Object System.Drawing.Size($ListViewLocationWidth,$ListViewLocationHeight) $ListViewSizeWidth = $FormWidth * 0.93 $ListViewSizeHeight = $FormHeight * 0.05 $objListView.Size = New-Object System.Drawing.Size($ListViewSizeWidth,$ListViewSizeHeight) $objListView.Name = 'List of RDS Users' $objListView.CheckBoxes = $false $objListView.GridLines = $True $objListView.visible = $true $objListView.Sorting = 1 $objListView.View = 1 $objListView.FullRowSelect = $true $UserNameColumn = $objListView.columns.add('UserName Name') $UserNameColumn.Width = 80 $RDSStatus =$objListView.columns.add('RDS Status') $RDSStatus.Width = 80 $RDSServer =$objListView.columns.add('RDS Server') $RDSServer.Width = 80 Function FillListBox { $LoggedOnUsers = Get-RDUserSession -ConnectionBroker "$connectionBroker" -CollectionName "$SessionHostCollection" | Sort-Object -Property UserName ForEach ($user in $LoggedOnUsers) { $RDS_User = "{0,-10}" -f $user.UserName.PadRight(10) SWITCH ($user.SessionState) { "STATE_CONNECTED" { $RDS_Status = "ACTIVE "} "STATE_DISCONNECTED" { $RDS_Status = "DISCONNECTED"} } $RDS_Server = [string] $user.ServerName $TheListEntry = $RDS_User + "| " + $RDS_Status + "; " + $RDS_Server.SubString(0,10) [void] $objListBox.Items.Add($TheListEntry) $item1 = new-object System.Windows.Forms.ListViewItem("$RDS_User",0) $item1.Name = "$RDS_User" $item1.SubItems.add("$RDS_Status") $item1.SubItems.add("$($RDS_Server.SubString(0,10))") [void] $objListView.items.add($item1) } } Note: $ItemsSelected = $objListView.SelectedItems Should return an array of the selected items So you can get info out something like : foreach($thing in $ ItemsSelected) { $Username = "$($thing.name)" $RDSStatus = $($thing.SubItems[0].Text) $RDSServer = $($thing.SubItems[1].Text) # DO what ever... } Note I have not tested this at all... but its more or less copy and paste from code I do use... ________________________________ MAGNUS TVETEN SERVER SUPPORT ENGINEER CO CITRIX/SERVER Insurance Australia Group Limited Lvl 1, 23 Lakeside Drive, Burwood Burwood East VIC 3151 Australia T +61 3 8804 3226 M +61 411 010 460 E magnus.tve...@iag.com.au www.iag.com.au PLEASE CONSIDER THE ENVIRONMENT BEFORE PRINTING THIS EMAIL. ________________________________ From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On Behalf Of Michael Leone Sent: Wednesday, 1 October 2014 6:33 AM To: powershell@lists.myitforum.com Cc: ntsys...@lists.myitforum.com Subject: [powershell] Formatting a ListBox I am not a .Net programmer. But I came across a script online that I find useful, and now I am trying to modify it a bit. What the script does is put a listbox on a form; the listbox is populated with an entry I am trying to construct (by which I mean - I am concatenating 3 string variables together, and adding that as the list entry. Here's the problem: The onscreen formatting needs to be a fixed width, and what I am ending up with looks like this: [Inline image 1] See how it's wandering? I want a fixed font onscreen, and I can't seem to figure out how to get it to do that. I am padding the individual components to the widths I want, but it still shows onscreen like that. Here's the code snippets: $FormWidth = 300 # 300 $FormHeight = 500 # 320 $objForm = New-Object System.Windows.Forms.Form $objForm.Text = "Select user(s)" $objForm.Size = New-Object System.Drawing.Size($FormWidth,$FormHeight) $objForm.StartPosition = "CenterScreen" ## ----- Make the ListBox ----- $objListBox = New-Object System.Windows.Forms.ListBox $objListBox.Name = "List of RDS Users" $ListBoxLocationWidth = $FormWidth * 0.03 $ListBoxLocationHeight = $FormHeight * 0.09 $objListBox.Location = New-Object System.Drawing.Size($ListBoxLocationWidth,$ListBoxLocationHeight) $ListBoxSizeWidth = $FormWidth * 0.93 $ListBoxSizeHeight = $FormHeight * 0.05 $objListBox.Size = New-Object System.Drawing.Size($ListBoxSizeWidth,$ListBoxSizeHeight) $objListBox.Height = $FormHeight * 0.41 $objListBox.ScrollAlwaysVisible = $true $objListBox.Font = "Courier Regular" $objListBox.SelectionMode = "MultiExtended" Function FillListBox { $LoggedOnUsers = Get-RDUserSession -ConnectionBroker "$connectionBroker" -CollectionName "$SessionHostCollection" | Sort-Object -Property UserName ForEach ($user in $LoggedOnUsers) { $RDS_User = "{0,-10}" -f $user.UserName.PadRight(10) SWITCH ($user.SessionState) { "STATE_CONNECTED" { $RDS_Status = "ACTIVE "} "STATE_DISCONNECTED" { $RDS_Status = "DISCONNECTED"} } $RDS_Server = [string] $user.ServerName $TheListEntry = $RDS_User + "| " + $RDS_Status + "; " + $RDS_Server.SubString(0,10) [void] $objListBox.Items.Add($TheListEntry) } I'm sure it's simple, but it's eluding my fried brain today. Clues, anyone? ================================================ Did you know you can also post and find answers on PowerShell in the forums? http://www.myitforum.com/forums/default.asp?catApp=1 _______________________________________________________________________________________ The information transmitted in this message and its attachments (if any) is intended only for the person or entity to which it is addressed. The message may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information, by persons or entities other than the intended recipient is prohibited. If you have received this in error, please contact the sender and delete this e-mail and associated material from any computer. The intended recipient of this e-mail may only use, reproduce, disclose or distribute the information contained in this e-mail and any attached files, with the permission of the sender. This message has been scanned for viruses. _______________________________________________________________________________________ ================================================ Did you know you can also post and find answers on PowerShell in the forums? http://www.myitforum.com/forums/default.asp?catApp=1