Page 1 of 2

Select-Object output into ListView?

Posted: Fri Aug 03, 2018 7:37 am
by thatsameer

I firstly request your patience and politeness as I haven't used message boards before & also I'm not a super expert at PowerShell. So if I post something wrong, apologies, but please be nice.

I created a form on Powershell Studio 2018.
(I'm using the Quest ActiveRoles Management Shell to work with Active Directory.)

I type in the name of a Active Directory user in a richtextbox and click 'Go' (e.g. Sam)

On the 'Go' button I've added this code:

$username = $richtextbox1.Text
$usernameresults = get-qaduser $username | Select-Object ntaccountname, accountislockedout

In a normal Powershell ISE console, running the second line of the above script replacing $username with an actual name found in Active Directory returns a table in the output section with the headings 'NTAccountName' and 'AccountisLockedOut'.
This displays all users with the name I inputted (e.g. Sam) with the account name and locked status. Similar table below:

NTAccountName AccountIsLockedOut
------------- ------------------
Domain1\SamJohn False
Domain1\SammyJohn True

TableIWant.png (12.97 KiB) Viewed 694 times

I put this into a $usernameresults variable in my Sapien Form because I want to push this table into a ListView. Similar to this image:

Details-View.png (6.72 KiB) Viewed 694 times

I experimented with the line:


But obviously as it was an experiment it didn't work.

My question is, how do I get that table above (which is put into variable $usernameresults) into a box in Sapien Powershell so that I can then mouse click which user I want to select and do the rest of the code from there? When I click select the username I want to feed the "NTAccountName" for the rest of my script.

I hope this makes sense... I thought ListView is the way but it may or may not be.

Thanks in advance.

Re: Select-Object output into ListView?

Posted: Fri Aug 03, 2018 7:51 am
by jvierra
Try this to understand how to use a ListView: ... ew-control

To add items use the provided function:

Add-ListViewItem -ListView $listview1 -Items $usernameresults -Group $listview1.Groups[0] -SubItems ntaccountname, accountislockedout

Re: Select-Object output into ListView?

Posted: Fri Aug 03, 2018 8:39 am
by thatsameer
Hi, Thank you for the very quick reply.

I have my ListView set to SmallIcon and checkbox on.

I added the line you mentioned below and it's getting there. I had to remove the

| select-object ntaccountname, accountislockedout

for the listview to display properly.

This is what it looks like (code behind)

Gettingthere.png (53.04 KiB) Viewed 674 times

It's so very close to what I wanted. I would really like to know how to create two columns "NTaccountname" and "Accountislockedout" populated with the information piped in from $usernameresults (just like in the screenshot of the first post).

The -Subitems ntaccountname, accountislockedout doesn't make a different to the current output in the listview.

I tried reading the link but either I missed it or I don't understand how to do it from reading that page.

This help is very much appreciated.


Re: Select-Object output into ListView?

Posted: Fri Aug 03, 2018 9:13 am
by jvierra
Read the fist link I posted to find out what things are available. To have columns just define columns.

Re: Select-Object output into ListView?

Posted: Fri Aug 03, 2018 1:59 pm
by thatsameer
Thank you my friend! I got it working exactly how I wanted it by actually spending time reading the spotlight :D

I will post screenshots and what I did in a bit, in case anyone else needs some assistance...

Re: Select-Object output into ListView?

Posted: Fri Aug 03, 2018 2:48 pm
by jvierra
Great. Working examples are always useful.

Re: Select-Object output into ListView?

Posted: Mon Aug 06, 2018 7:38 am
by thatsameer
So to conclude this topic, this is what I did to make the above code work.

I read on the spotlight link mentioned above:
"In the Details view the Subitems will be displayed only if Columns are defined."

That was a critical piece I was missing from my form.

So I added 2 columns in Powershell studio, and modified the script line to read:

Add-ListViewItem -ListView $listview1 -Items $usernameresults.ntaccountname -Group $listview1.Groups[0] -SubItems $usernameresults.accountislockedout

I realised that "-Items" appends to the first defined column and "-Subitems" appends to the column after.

Thus resulting in the following:
SearchResultsWorking.png (6.1 KiB) Viewed 569 times

Thanks for the assistance.

Re: Select-Object output into ListView?

Posted: Wed Aug 08, 2018 5:06 am
by thatsameer

It seems I spoke to soon. I actually need more assistance.

It seems -Subitems only takes the first result and displays it in the column. It does not take all the rows into account. It only takes the first row and shows the whole column with the first row only.

What I mean by this -
If I add the line like this, so the first column shows the username and second column shows locked out status, it looks like this:
Add-ListViewItem -ListView $listview1 -Items $usernameresults.ntaccountname -Group $listview1.Groups[0] -SubItems $usernameresults.accountislockedout

Username.png (32.65 KiB) Viewed 543 times

Looks like everything is working... however, one of those users account is locked so it should return as "True".

How can I confirm this, I swapped -Items with -Subitems so the line now reads:
Add-ListViewItem -ListView $listview1 -Items $usernameresults.accountislockedout -Group $listview1.Groups[0] -SubItems $usernameresults.ntaccountname

UsernameinSubItem.png (32.05 KiB) Viewed 543 times

As you can see, the first column (-Item) displays each row independently taken from the results and shows a "True" result. But the second column (-Subitems) shows only the first username from the result, copying the 1st line into all the lines in that column.

My question is, how can I get -Subitems to act like -Items? Each row displaying the result and not copying the first row to all rows?

I've read the spotlight many times but I just cant see what I'm doing wrong

After this is solved, my entire script will be complete!

Re: Select-Object output into ListView?

Posted: Wed Aug 08, 2018 5:34 am
by jvierra
This is one reason why I never use the function. It is not well behaved and doesn't work as advertised.

Here is an example of how to use it in a way that works.

Re: Select-Object output into ListView?

Posted: Wed Aug 08, 2018 5:56 am
by jvierra
Here is a more complete demo of how to use a ListBox.