Hey, Scripting Guy! How Can I Read a Text File and Update a List Box?

ScriptingGuy1

Bookmark and Share 

 

Hey, Scripting Guy! QuestionHey, Scripting Guy! I liked your article about creating a list box with Windows PowerShell, but to be honest, that is of limited use to me because I often need to work with different servers. I would like to be able to load a text file and have it populate the list box. Is that too hard to do?

— RR

 

Hey, Scripting Guy! AnswerHello RR,

Microsoft Scripting Guy Ed Wilson here. Spring is coming to Charlotte, North Carolina, in the United States. This means the rains are coming, and umbrellas will be our constant companions outside. In honor of the spring, I am listening to Tom Jones on my Zune HD and sipping a wonderful cup of Darjeeling tea and munching on some Irish cheddar cheese and walnuts. The key to brewing Darjeeling tea is to let it steep for 3–4 minutes, and then to remove the tea basket.

Make sure the fresh spring water comes to a vigorous boil prior to adding the tea. Do not use more than two teaspoons of tea leaves for the pot. I do not reuse my tea leaves; instead, I prefer to use fresh tea leaves for each pot of tea. When properly brewed, Darjeeling tea is my favorite black tea. When brewed improperly, I would just as soon have a soda pop, and I do not generally drink soda pop. I was talking to Sean in Canada this morning on Twitter, and we began to discuss poutine. He said he had never eaten it, and I said I fell in love with when I was in Quebec City teaching a Windows PowerShell class. In addition to eating poutine, I took an icebreaker across the Saint Lawrence River and was able to get some awesome pictures, such as the one following this paragraph.

Photo of Saint Lawrence River

 

Well, RR, the cool damp spring air and the invigorating Darjeeling tea and Tom Jones have me sufficiently motivated to open the Windows PowerShell ISE and begin playing around. It is entirely possible to write graphical Windows PowerShell scripts by hand. It is a lot of work, but can be kind of fun. I am not in the mood for that much fun, however, so I will begin the script by creating the basic Windows Form by using the free PrimalForms Community Edition utility that is available from SAPIEN Technologies. For more information about using PrimalForms, look at Monday’s Hey, Scripting Guy! Blog post.

First, open PrimalForms and change the name of the Windows Form to “List Box Fill Example.” The Text property of the Form class is modified under the Appearance section, as shown in the following image.

Image of Text property in Appearance section

Now add a ListBox control to the form by dragging it from the palate on the left side and dropping it on the form. You will want to change the name of the control to lb_Computers, so it will be easy to remember when you are using it later. The Name property of the control is under the Design section, as shown in the following image.

Image of Name property in Design section

You next need to add a TextBox control to the form. The TextBox will be used to supply the path to the text file that contains the names of all the computers you will populate inside the list box. Name the TextBox control tb_Path as shown in the following image.

Image of naming TextBox control

 

It is a good idea to use a Label control under the TextBox so you know what you are supposed to type in the TextBox. Name the Label control lbl_Path and give it a text value of something like “Path to File” or simply “Path,” as shown in the following image. The Text property of the Label control is found under the Appearance section.

Image of Text property of Label control

Use a Button control to read the text file and update the ListBox with the names of all the computers. The On_Click event of the button will be used to initiate the reading of the file and the populating of the ListBox. Name the button btn_Update, as shown in the following image, and assign the word “Update” to the Text property of the control.

Image of naming the button

Save the form project, and generate the Windows PowerShell code to create the Windows Form. Open the Windows PowerShell code, and go to the #TODO section of the button click event handler in the script. The first thing to do is to use the Get-Content cmdlet to read the contents of the text file whose path was typed into the tb_Path text box. This will be stored in the $tb_Path.Text property. In reality, you would want to first use the Test-Path cmdlet to ensure that the text file actually exists, and if it does not exist, change the label control text to indicate that the file is missing. But this is just a demonstration script, and we will ignore that for today. This is seen here:

$computerNames = Get-Content -Path $tb_Path.Text

Next we want to update the ListBox control with the contents of the text file. To do this, first call the BeginUpdate method—you do this is for performance reasons. Now, it is a simple matter of using a foreach statement to iterate through the collection of computer names and to call the add method to add them to the list box. When you are done, make sure you call the EndUpdate method . The complete code for this is seen here:

#TODO: Place custom script here

 $computerNames = Get-Content -Path $tb_Path.Text

 $lb_Computers.BeginUpdate()

      foreach($computer in $computerNames)

        {

         $lb_Computers.Items.add($computer)

        }

 $lb_Computers.EndUpdate()

When the script runs, the Windows Form shown in the following image appears. Type a path to a text file with computer names on single lines, click Update, and the computer names appear in the list box. As seen in yesterday’s Hey, Scripting Guy! post, it is a simple matter to retrieve a selected value from a list box.

Image of Windows Form that appears when script runs

The complete ListBox2.ps1 script has been posted on the Scripting Guys Script Repository.

 

RR, that is all there is to reading a text file and updating a list box. Graphical Windows PowerShell Week will continue tomorrow.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

 

0 comments

Discussion is closed.

Feedback usabilla icon