Functions/FileSystem/Select-SaveFilePath.ps1
function Select-SaveFilePath { [CmdletBinding()] param ( [Parameter(Mandatory=$false)] [string] $StartPath = "C:\", [Parameter(Mandatory=$false)] [string] $Description = "Select a location to save your file to", [Parameter(Mandatory=$false)] [string] $FileName = "", [Parameter(Mandatory=$false)] [ValidateSet(".png",".jpeg",".jpg",".bmp",".txt",".csv")] [string] $Extension ) # Establish Filter based on presence of an extension $FilterOrder = if ($Extension) { switch ($Extension) { ".png" {"Image files (*.png;*.jpeg;*.jpg;*.bmp)|*.png;*.jpeg;*.jpg;*.bmp|All files (*.*)|*.*";} ".jpeg" {"Image files (*.png;*.jpeg;*.jpg;*.bmp)|*.png;*.jpeg;*.jpg;*.bmp|All files (*.*)|*.*";} ".jpg" {"Image files (*.png;*.jpeg;*.jpg;*.bmp)|*.png;*.jpeg;*.jpg;*.bmp|All files (*.*)|*.*";} ".bmp" {"Image files (*.png;*.jpeg;*.jpg;*.bmp)|*.png;*.jpeg;*.jpg;*.bmp|All files (*.*)|*.*";} ".txt" {"Text files (*.txt,*.csv)|*.txt;*.csv|All files (*.*)|*.*";} ".csv" {"Text files (*.txt,*.csv)|*.txt;*.csv|All files (*.*)|*.*";} Default {"All files (*.*)|*.*";} } } else { "All files (*.*)|*.*|Text files (*.txt,*.csv)|*.txt;*.csv|Image files (*.png;*.jpeg;*.jpg;*.bmp)|*.png;*.jpeg;*.jpg;*.bmp"; } [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null [System.Windows.Forms.Application]::EnableVisualStyles() $browse = New-Object System.Windows.Forms.SaveFileDialog $browse.initialdirectory = $Startpath $browse.title = $Description $browse.FileName = $FileName $browse.DefaultExt = $Extension $browse.AddExtension = $true $browse.Filter = $FilterOrder $loop = $true while($loop) { if ($browse.ShowDialog() -eq "OK") { $loop = $false } else { write-host "User Cancelled Path Selection" -ForegroundColor Red return } } $browse.filename $browse.Dispose() } |