• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1798
  • Last Modified:

Powershell "read-host" options

Google showed me this function:
function Get-FileName {
$computer = Read-Host "Filename of computer names?"
return $computer

Open in new window

Which I love, but got me to thinking.  Is it possible to add to this in such a fashion where this is indeed the default (provide a text file).  But there is a check box or something where I could select it and put in a single server name, or paste a list of server names?

This is obviously part of a bigger script, but I needed to make a change to only one server today.  I had to go create a text file with the single server name, etc....  Just got me to thinking.
  • 2
1 Solution
You could do something like;
$computer = Get-Content "C:\temp\serverlist.txt"
If you want an input box, you can use this;

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Enter the user name in the space below:"

$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)

$objForm.Topmost = $True



$x is the value of whatever is entered in the box.

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now