{"id":601,"date":"2008-08-13T11:00:35","date_gmt":"2008-08-13T19:00:35","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/08\/13\/powershell-inputbox\/"},"modified":"2016-10-27T13:51:42","modified_gmt":"2016-10-27T20:51:42","slug":"powershell-inputbox","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/08\/13\/powershell-inputbox\/","title":{"rendered":"PowerShell InputBox"},"content":{"rendered":"<p><a href=\"http:\/\/www.sapien.com\/blog\/2008\/08\/12\/powershell-msgbox\" target=\"_blank\">The other day<\/a> I gave you a function to create a VBScript style message box in PowerShell. If you find yourself needing MsgBox in PowerShell, you&#8217;re likely going to want InputBox. This too is accomplished using the [microsoft.visualbasic.interaction] class. You&#8217;ll need to load the assembly first if you haven&#8217;t already.<\/p>\n<p><span style=\"color: #0000ff; font-family: Consolas;\">PS C:\\&gt; [reflection.assembly]::loadwithpartialname(&#8220;microsoft.visualbasic&#8221;) | Out-Null<\/span><\/p>\n<p><span style=\"color: #0000ff; font-family: Consolas;\">PS C:\\&gt; [microsoft.visualbasic.interaction]::Inputbox(&#8220;enter something&#8221;)<\/span><\/p>\n<p>As in VBScript you can also specify a title and a default value. Here&#8217;s a more complete function you could call and an example.<\/p>\n<div style=\"font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;\">\n<div style=\"font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\">\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   1:<\/span> Function Show-Inputbox {<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   2:<\/span>  Param([string]$message=$(Throw <span style=\"color: #006080;\">\"You must enter a prompt message\"<\/span>),<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   3:<\/span>        [string]$title=<span style=\"color: #006080;\">\"Input\"<\/span>,<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   4:<\/span>        [string]$<span style=\"color: #0000ff;\">default<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   5:<\/span>        )<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   6:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   7:<\/span>  [reflection.assembly]::loadwithpartialname(<span style=\"color: #006080;\">\"microsoft.visualbasic\"<\/span>) | Out-Null<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   8:<\/span>  [microsoft.visualbasic.interaction]::InputBox($message,$title,$<span style=\"color: #0000ff;\">default<\/span>)<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   9:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  10:<\/span> }<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  11:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  12:<\/span> $c=Show-Inputbox -message <span style=\"color: #006080;\">\"Enter a computername\"<\/span> `<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  13:<\/span> -title <span style=\"color: #006080;\">\"Computername\"<\/span> -<span style=\"color: #0000ff;\">default<\/span> $env:Computername<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  14:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  15:<\/span> <span style=\"color: #0000ff;\">if<\/span> ($c.Trim()) {<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  16:<\/span>   Get-WmiObject win32_computersystem -computer $c<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  17:<\/span>   }<\/pre>\n<\/div>\n<\/div>\n<p>Executing the sample code will give you this inputbox:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/08\/pshinputbox.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px;\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/08\/pshinputbox-thumb.png\" alt=\"pshinputbox\" width=\"373\" height=\"160\" border=\"0\" \/><\/a><\/p>\n<p>The inputbox&#8217;s return value is whatever you enter. What you need to do, as I&#8217;ve done, is add code to validate the data.<\/p>\n<p><span style=\"color: #0000ff; font-family: Consolas;\">$c=Show-Inputbox -message &#8220;Enter a computername&#8221; `<br \/>\n-title &#8220;Computername&#8221; -default $env:Computername <\/span><\/p>\n<p><span style=\"color: #0000ff; font-family: Consolas;\">if ($c.Trim()) {<br \/>\nGet-WmiObject win32_computersystem -computer $c<br \/>\n}<\/span><\/p>\n<p>In my example I only use the value in the Get-WMIObject expression if $c has a value. I used the Trim() method to remove any spaces. It&#8217;s possible a user could accidentally enter space somewhere so using Trim cleans that up.<\/p>\n<div id=\"scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:601494c8-b71a-4ab2-b6a6-db116a8c309e\" class=\"wlWriterSmartContent\" style=\"display: inline; margin: 0px; padding: 0px;\">\n<p>Download this sample <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/08\/show-inputbox.txt\" target=\"_blank\">here<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The other day I gave you a function to create a VBScript style message box in PowerShell. If you find yourself needing MsgBox in PowerShell, you&#8217;re likely going to want InputBox.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[24,25],"tags":[198,270,28,57,996],"class_list":["post-601","post","type-post","status-publish","format-standard","hentry","category-vbscript","category-windows-powershell","tag-function","tag-inputbox","tag-powershell","tag-scripting","tag-vbscript"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=601"}],"version-history":[{"count":1,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/601\/revisions"}],"predecessor-version":[{"id":12767,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/601\/revisions\/12767"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}