Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active December 1, 2025 21:55
Show Gist options
  • Select an option

  • Save jdhitsolutions/831ed85a533101b3ef38db7602c4ba11 to your computer and use it in GitHub Desktop.

Select an option

Save jdhitsolutions/831ed85a533101b3ef38db7602c4ba11 to your computer and use it in GitHub Desktop.
Select files to delete in PowerShell. This requires the pwshSpectreConsole PowerShell module and PowerShell 7.
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.
Empty File#requires -version 7.5
#requires -module pwshSpectreConsole
Function Select-ToDelete {
<#
.SYNOPSIS
Select files to delete.
.DESCRIPTION
Pipe files to this command which will use SpectreConsole to present a choice menu. The command has an automatic timeout of 15 seconds.
.PARAMETER InputObject
Piped file objects to delete.
.PARAMETER TimeOut
The timeout value in seconds if you don't select anything.
.EXAMPLE
PS C:\> dir c:\temp -file | Select-ToDelete
This prompt times out in 15 seconds...
Select file(s) to delete from C:\temp:
> [ ] a.jpg
[ ] ADSync.zip
[ ] ADUser-Replacement.psd1
[ ] ADUser.psd1
[ ] foo.ps1
[ ] demo.ps1
.NOTES
This command has an alias of std.
.INPUTS
FileInfo
.OUTPUTS
None
.LINK
Remove-Item
Read-SpectreMultiSelection
#>
[CmdletBinding(SupportsShouldProcess)]
[alias('std')]
[OutputType("None")]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo[]]$InputObject,
[Parameter(HelpMessage = "Specify a timeout value in seconds.")]
[int32]$TimeOut = 15
)
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-Verbose "Selecting from $($list.count) file(s)"
#parameters to splat to Read-SpectreMultiSelection
$splat = @{
Message = "`nSelect file(s) to delete from [SpringGreen1]$($list[0].DirectoryName)[/]:"
Choices = $list
ChoiceLabelProperty = 'Name'
Color = 'Violet'
AllowEmpty = $True
PageSize = 10
TimeoutSeconds = $TimeOut
}
Read-SpectreMultiSelection @splat | Remove-Item
}
} #end
}
@jdhitsolutions
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment