Advertisement
atheos42

Show-FileBrowser

Oct 3rd, 2022 (edited)
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 3.08 KB | Source Code | 0 0
  1. function Show-FolderBrowser
  2. {      
  3.     Param ( [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$Title='Browse For Folder',
  4.             [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$SelectedPath = $(Split-Path $psISE.CurrentFile.FullPath),
  5.             [Parameter(Mandatory=0,ValueFromPipeline=1)][switch]$ShowNewFolderButton)
  6.  
  7.     $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog;
  8.     $FolderBrowser.Description = $Title
  9.     $FolderBrowser.ShowNewFolderButton = $ShowNewFolderButton
  10.     $FolderBrowser.SelectedPath = $SelectedPath
  11.     $sp = $null
  12.  
  13.     $caller = [System.Windows.Forms.NativeWindow]::new()
  14.     $caller.AssignHandle([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle)
  15.  
  16.     if (($FolderBrowser.ShowDialog($caller)) -eq [System.Windows.Forms.DialogResult]::OK.value__)
  17.     {
  18.         $sp = $FolderBrowser.SelectedPath
  19.     }
  20.  
  21.     #Cleanup
  22.     $FolderBrowser.Dispose()
  23.  
  24.     return $sp
  25. }
  26.  
  27. function Show-FileBrowser
  28. {
  29.     Param ( [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$Title='Browse For File',
  30.             [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$InitialDirectory = $(Split-Path $psISE.CurrentFile.FullPath),
  31.             [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$Filter = 'All files (*.*)|*.*'
  32.     )
  33.  
  34.     $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
  35.     $FileBrowser.Title = $Title
  36.     $FileBrowser.InitialDirectory = $InitialDirectory
  37.     $FileBrowser.Filter = $Filter
  38.     $fn = $null
  39.  
  40.     $caller = [System.Windows.Forms.NativeWindow]::new()
  41.     $caller.AssignHandle([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle)
  42.  
  43.     if (($FileBrowser.ShowDialog($caller)) -eq [System.Windows.Forms.DialogResult]::OK.value__)
  44.     {
  45.         $fn = $FileBrowser.FileName
  46.     }
  47.  
  48.     #Cleanup
  49.     $FileBrowser.Dispose()
  50.  
  51.     return $fn
  52. }
  53.  
  54. function Show-SaveFileBrowser
  55. {
  56.     Param ( [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$Title='Save File',
  57.             [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$InitialDirectory = $(Split-Path $psISE.CurrentFile.FullPath),
  58.             [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$Filter = 'All files (*.*)|*.*'
  59.     )
  60.  
  61.     $SaveBrowser = New-Object System.Windows.Forms.SaveFileDialog
  62.     $SaveBrowser.Title = $Title
  63.     $SaveBrowser.InitialDirectory = $InitialDirectory
  64.     $SaveBrowser.Filter = $Filter
  65.     $fn = $null
  66.  
  67.     $caller = [System.Windows.Forms.NativeWindow]::new()
  68.     $caller.AssignHandle([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle)
  69.  
  70.     if (($SaveBrowser.ShowDialog($caller)) -eq [System.Windows.Forms.DialogResult]::OK.value__)
  71.     {
  72.         $fn = $SaveBrowser.FileName
  73.     }
  74.  
  75.     #Cleanup
  76.     $SaveBrowser.Dispose()
  77.  
  78.     return $fn
  79. }
  80.  
  81. $documents = [Environment]::GetFolderPath("MyDocuments")
  82.  
  83. Clear-Host
  84.  
  85. Show-FolderBrowser -SelectedPath $documents -ShowNewFolderButton
  86. Show-FileBrowser  -InitialDirectory $documents -Filter 'PowerShell files (*.ps1)|*.ps1'
  87. Show-SaveFileBrowser -InitialDirectory $documents -Filter 'PowerShell files (*.ps1)|*.ps1'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement