{"id":51123,"date":"2010-03-03T00:01:00","date_gmt":"2010-03-03T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/03\/03\/hey-scripting-guy-how-can-i-read-a-text-file-and-update-a-list-box\/"},"modified":"2010-03-03T00:01:00","modified_gmt":"2010-03-03T00:01:00","slug":"hey-scripting-guy-how-can-i-read-a-text-file-and-update-a-list-box","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-read-a-text-file-and-update-a-list-box\/","title":{"rendered":"Hey, Scripting Guy! How Can I Read a Text File and Update a List Box?"},"content":{"rendered":"<p class=\"MsoNormal\"><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>&nbsp;<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\">Hey, 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?<\/p>\n<\/p>\n<p class=\"MsoNormal\">&#8212; RR<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello RR, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. Spring is coming to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Charlotte_nc\"><font face=\"Segoe\">Charlotte, North Carolina<\/font><\/a>, 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 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Tom_Jones_(singer)\"><font face=\"Segoe\">Tom Jones<\/font><\/a> on my <a href=\"http:\/\/zune.net\/en-us\/products\/zunehd\/default.htm\"><font face=\"Segoe\">Zune HD<\/font><\/a> and sipping a wonderful cup of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Darjeeling_tea\"><font face=\"Segoe\">Darjeeling tea<\/font><\/a> and munching on some Irish cheddar cheese and walnuts. The key to brewing Darjeeling tea is to let it steep for 3&ndash;4 minutes, and then to remove the tea basket. <\/p>\n<p class=\"MsoNormal\">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 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Soft_drink\"><font face=\"Segoe\">soda pop<\/font><\/a>, and I do not generally drink soda pop. I was talking to Sean in Canada this morning on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\"><font face=\"Segoe\">Twitter<\/font><\/a>, and we began to discuss <a href=\"http:\/\/en.wikipedia.org\/wiki\/Poutine\"><font face=\"Segoe\">poutine<\/font><\/a>. 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 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Saint_Lawrence_River\"><font face=\"Segoe\">Saint Lawrence River<\/font><\/a> and was able to get some awesome pictures, such as the one following this paragraph.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Photo of Saint Lawrence River\" alt=\"Photo of Saint Lawrence River\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0303\/hsg-03-03-10-01.jpg\" width=\"600\" height=\"450\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">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 <a href=\"http:\/\/www.primaltools.com\/downloads\/communitytools\/\">PrimalForms Community Edition<\/a> utility that is available from SAPIEN Technologies. For more information about using PrimalForms, look at <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/03\/01\/hey-scripting-guy-march-1-2010.aspx\">Monday&rsquo;s Hey, Scripting Guy! Blog post<\/a>.<\/p>\n<p class=\"MsoNormal\">First, open PrimalForms and change the name of the Windows Form to &ldquo;List Box Fill Example.&rdquo; The <strong>Text <\/strong>property of the <b>Form<\/b> class is modified under the <b>Appearance<\/b> section, as shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Text property in Appearance section\" alt=\"Image of Text property in Appearance section\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0303\/hsg-03-03-10-02.jpg\" width=\"600\" height=\"426\"><\/p>\n<p class=\"Fig-Graphic\">Now add a <b>ListBox<\/b> 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 <b>lb_Computers<\/b>, so it will be easy to remember when you are using it later. The <b>Name<\/b> property of the control is under the <b>Design<\/b> section, as shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Name property in Design section\" alt=\"Image of Name property in Design section\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0303\/hsg-03-03-10-03.jpg\" width=\"600\" height=\"426\"><\/p>\n<p class=\"Fig-Graphic\">You next need to add a <b>TextBox<\/b> control to the form. The <b>TextBox<\/b> 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 <b>TextBox<\/b> control <b>tb_Path<\/b> as shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of naming TextBox control\" alt=\"Image of naming TextBox control\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0303\/hsg-03-03-10-04.jpg\" width=\"600\" height=\"426\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">It is a good idea to use a <b>Label<\/b> control under the <b>TextBox<\/b> so you know what you are supposed to type in the <b>TextBox<\/b>. Name the <b>Label<\/b> control <b>lbl_Path<\/b> and give it a text value of something like &ldquo;Path to File&rdquo; or simply &ldquo;Path,&rdquo; as shown in the following image. The <b>Text<\/b> property of the <b>Label<\/b> control is found under the <b>Appearance<\/b> section. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Text property of Label control\" alt=\"Image of Text property of Label control\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0303\/hsg-03-03-10-05.jpg\" width=\"600\" height=\"426\"><\/p>\n<p class=\"Fig-Graphic\">Use a <b>Button<\/b> control to read the text file and update the <b>ListBox<\/b> with the names of all the computers. The <b>On_Click<\/b> event of the button will be used to initiate the reading of the file and the populating of the <b>ListBox<\/b>. Name the button <b>btn_Update<\/b>, as shown in the following image, and assign the word &ldquo;Update&rdquo; to the <b>Text<\/b> property of the control. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of naming the button\" alt=\"Image of naming the button\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0303\/hsg-03-03-10-06.jpg\" width=\"600\" height=\"426\"><\/p>\n<p class=\"Fig-Graphic\">Save the form project, and generate the Windows PowerShell code to create the Windows Form. Open the Windows PowerShell code, and go to the <b>#TODO<\/b> section of the button click event handler in the script. The first thing to do is to use the <b>Get-Content<\/b> cmdlet to read the contents of the text file whose path was typed into the <b>tb_Path<\/b> text box. This will be stored in the <b>$tb_Path.Text<\/b> property. In reality, you would want to first use the <b>Test-Path<\/b> 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:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$computerNames = Get-Content -Path $tb_Path.Text<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Next we want to update the <b>ListBox<\/b> control with the contents of the text file. To do this, first call the <b>BeginUpdate<\/b> method&mdash;you do this is for performance reasons. Now, it is a simple matter of using a <b>foreach<\/b> statement to iterate through the collection of computer names and to call the <b>add<\/b> method to add them to the list box. When you are done, make sure you call the <b>EndUpdate<\/b> method . The complete code for this is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">#TODO: Place custom script here<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$computerNames = Get-Content -Path $tb_Path.Text<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$lb_Computers.BeginUpdate()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>foreach($computer in $computerNames)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp; <\/span>{<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp; <\/span>$lb_Computers.Items.add($computer)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp; <\/span>}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$lb_Computers.EndUpdate()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">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 <b>Update<\/b>, and the computer names appear in the list box. As seen in <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/03\/02\/hey-scripting-guy-march-2-2010.aspx\"><span>yesterday&rsquo;s Hey, Scripting Guy! post<\/span><\/a>, it is a simple matter to retrieve a selected value from a list box. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Windows Form that appears when script runs\" alt=\"Image of Windows Form that appears when script runs\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0303\/hsg-03-03-10-07.jpg\" width=\"300\" height=\"198\"><\/p>\n<p class=\"MsoNormal\">The complete ListBox2.ps1 script has been posted on the <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/f10e344a-ba60-40b8-89e0-01fdb262440f\"><font face=\"Segoe\">Scripting Guys Script Repository<\/font><\/a>. <\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">RR, that is all there is to reading a text file and updating a list box. Graphical Windows PowerShell Week will continue tomorrow. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p><b><span><\/span><\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, 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. [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[71,3,4,45],"class_list":["post-51123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-graphical","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Hey, 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. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=51123"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51123\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=51123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}