Monday, December 1, 2014

File Selection Input Window

Sometimes in my scripting ways I need to solicit a location from a user with for reading or writing data. Below is a simple function I have used when I need a user to select a Comma Separated Values file. It creates a simple dialogue box showing .CSV files in a particular location. It will either return the selected file name or an error. It is a simple starting point that can be expanded upon as needed.


Function Select-FileWindow
{
      param(
      [string]$Title = "Select CSV",
      [string]$Directory = "C:",
      [string]$Filter = "CSV Files (*.csv)|*.csv"
      )
     
      [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms"| Out-Null
      $objForm = New-Object System.Windows.Forms.OpenFileDialog
      $objForm.InitialDirectory = $Directory
      $objForm.Filter = $Filter
      $objForm.Title = $Title
      $objForm.ShowHelp = $true
      $Show = $objForm.ShowDialog()
      If ($Show -eq "OK")
      {
            Return $objForm.FileName
      }
      Else
      {
            Write-Error "Operation cancelled by user."
      }
}

No comments:

Post a Comment