Last active
December 1, 2025 22:04
-
-
Save jdhitsolutions/0e9c31959f7067a00ac8f565a8a5e242 to your computer and use it in GitHub Desktop.
Select files from a list generated by the pwshSpectreConsole module.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MIT License | |
| Copyright (c) 2025 JDH Information Technology Solutions, Inc. | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in | |
| all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #requires -version 7.5 | |
| #requires -module pwshSpectreConsole | |
| Function Out-SelectFile { | |
| <# | |
| .SYNOPSIS | |
| Select files from a SpectreConsole list | |
| .DESCRIPTION | |
| Select files using Read-SpectreMultiSelection from the pwshSpectreConsole module. Selected objects will be written to the pipeline. | |
| The filenames must be unique. | |
| .EXAMPLE | |
| PS C:> dir c:\temp -file | Out-SelectFile | Remove-Item | |
| .LINK | |
| Read-SpectreMultiSelection | |
| #> | |
| [CmdletBinding()] | |
| [alias('osf')] | |
| param ( | |
| [Parameter(Mandatory, ValueFromPipeline)] | |
| [ValidateNotNullOrEmpty()] | |
| [System.IO.FileInfo[]]$InputObject, | |
| [Parameter(HelpMessage = "Specify the select page size greater or equal to 5.")] | |
| [ValidateScript({$_ -gt 5},ErrorMessage = "Specify a page size >= 5")] | |
| [int32]$PageSize = 25, | |
| [Parameter(HelpMessage = "Specify a pwshSpectreConsole color")] | |
| [alias("Color")] | |
| [string]$SelectionColor = "Violet", | |
| [Parameter(HelpMessage = "Specify a timeout value in seconds.")] | |
| [ValidateScript({$_ -gt 0})] | |
| [int]$Timeout | |
| ) | |
| Begin { | |
| #initialize an empty list to hold the files | |
| $list = [System.Collections.Generic.List[System.IO.FileInfo]]::new() | |
| } #begin | |
| Process { | |
| ` #add each file to the list | |
| foreach ($file in $InputObject) { | |
| $list.Add($file) | |
| } | |
| } #process | |
| End { | |
| if ($list.Count -gt 0) { | |
| Write-Information $list | |
| #get the top-most directory path | |
| $top = $list.DirectoryName | Sort-Object Length | Select-Object -first 1 | |
| $splat = @{ | |
| Message = "`nSelect file(s) to process from [SpringGreen1]$($top)[/]:" | |
| Choices = $list | |
| ChoiceLabelProperty = 'Name' | |
| Color = $SelectionColor | |
| AllowEmpty = $True | |
| PageSize = $PageSize | |
| } | |
| If ($Timeout -gt 0) { | |
| $splat.Add('TimeoutSeconds',$TimeOut) | |
| } | |
| Read-SpectreMultiSelection @splat | |
| } | |
| } #end | |
| } | |
| Register-ArgumentCompleter -CommandName Out-SelectFile -ParameterName SelectionColor -ScriptBlock { | |
| param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) | |
| [Spectre.Console.Color] | | |
| Get-Member -Static -Type Properties | | |
| Where-Object name -like "$WordToComplete*"| | |
| Select-Object -ExpandProperty Name | | |
| ForEach-Object { | |
| $show = "[$_]$($_)[/]" | Out-SpectreHost | |
| [System.Management.Automation.CompletionResult]::new($_, $show, 'ParameterValue', $_) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Selected file objects will be written to the pipeline