PowerShell, Scripting
comments 4

PowerShell file browser to demonstrate Windows forms ListView control

It has been a while since I blogged something here. I have been busy with some work and not able to spend time writing on this blog. Anyway, today, I want to share a demo script I created to demonstrate using ListView control within PowerShell. I created this a while back and did not really get a chance to write about it. I made a few updates to the GUI and just published it on TechNet script gallery. Here is how it looks.   

PoSh Browser

PoSh Browser

This one is really straight-forward to create. There is no complex stuff involved as in the case of DataGrid or TreeView controls.  First step is to use Sapien PrimalForms to create the GUI form and add the necessary controls as needed. The scripting guys blog has a pretty good article on this. 

Windows forms listview supports different modes and in this example we are only looking at the LargeIcon view. Similar to Windows explorer, we can provide an option to the end user to select the view they want and refresh the contents of our listview control. These views include SmallIcon, details, list, and Tile. The following line (144) in the script sets the LargeIcon view.  $listView1.View = 0 

So, once you have the controls in place, use the export PowerShell option on the PrimalForms toolbar to save the form as a PS1 script. Now, we need to add the necessary events and logic to display the files/folders in the list view. To do that, 

Add form onload event 

In the above snippet, I am using a custom function that takes “c:\” as an argument. You can replace c:\ with any drive or folder path you want. You can even capture that from a combobox selection of drives available on the system. For the sake of simplicity, I hard coded the drive letter. Now, let us look at the Refresh-ListView function 

Again, there is no rocket science here. There are several properties that are associated with a list view control. We just make use of that to populate the items in the listview. For that we use Get-ChildItem cmdlet to get the list of folders & files within the given path and then set $listView1.Items. Once, the ListView is populated we need to have a track of the parent folder or path we are coming from. That is exactly what we capture using  $script:previousItem = (Get-Item $path).PSParentPath. We make sure we associated proper icons for the list view display. This icon image list is also created within the script. 

Once the initial form creation and listview population is complete, we need to enable the back button and close button. Enabling close button is very simple. We just have to put $form1.Close() in the button click event. For enabling the back button, we will reuse the refresh-listview function and give it the value of $script:previousItem. Simple.! 

Enabling double-click functionality 

This is also simple. We just trap the double-click event and run the code specified in the double-click event. Here is the code for doing that  

This is it. We have the complete form built and when you run this you will see an explorer style GUI to navigate through files and folders. This can be extended further to make some cool improvements. I will be using this in the PS Remote File Explorer GUI I created sometime back. Stay tuned.   

You can also download the script and associated icon files @

Filed under: PowerShell, Scripting


Ravikanth is a principal engineer and the lead architect for Microsoft and VMware virtualized and hybrid cloud solutions within the Infrastructure Solutions Group at Dell EMC. He is a multi-year recipient of Microsoft Most Valuable Professional (MVP) award in Windows PowerShell (CDM) and Microsoft Azure. Ravikanth is the author of Windows PowerShell Desired State Configuration Revealed (Apress) and leads Bangalore PowerShell and Bangalore IT Pro user groups. He can be seen speaking regularly at local user group events and conferences in India and abroad.