title: PowerShell Use File Dialog OR User Entry for Computer List
body: <p>When I write PowerShell scripts they are typically to be used against many computers.  Since we use a text file with a list of computers, I write my scripts with the following function:</p>
  
  <pre><code>Write-host "Select Text/CSV File with List of Computers"
  
  #Provides Dialog Box to select a file with list of computers.  File must contain only 1 of each of the Computer name(s) per line
  Function Get-OpenFile($initialDirectory)
  { 
      [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |Out-Null
  
      $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
      $OpenFileDialog.initialDirectory = $initialDirectory
      $OpenFileDialog.ShowDialog()
      $OpenFileDialog.filename
      $OpenFileDialog.ShowHelp = $true
  }
  
  $InputFile = Get-OpenFile
  If($InputFile -eq "Cancel"){
  Write-host "Canceled By User"
  exit}
  Else{
  $Computers = @(get-content -path $InputFile)
  }
  </code></pre>
  
  <p>If I'm writing a script to be used for a single computer, I use the following:</p>
  
  <pre><code>$computer = Read-Host "Enter hostname"
  </code></pre>
  
  <p>My question is, how can I write these scripts to do the following:</p>
  
  <ul>
  <li>A) the Get-OpenFile command to parse the computer names</li>
  <li>B) if the Get-OpenFile is canceled, have the script prompt for a computer name using Read-Host</li>
  <li>C) if the Read-Host entry is empty, then cancel the script</li>
  </ul>
  
  <p>Any ideas?</p>