Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
September 5, 2025

More SpectreConsole Scripting

Let's pick up where we left off in exploring ways to script with the pwshSpectreConsole module. The module has many commands that you can use to create rich console applications. I want to continue to look at how I am using the module to create a console-based pick list.

Select-ToDelete

Here's a variation on the code I demonstrated last time. PowerShell functions should only do one thing, so I am bending the guidelines a bit with these functions. Although, I could make an argument that this function is doing one thing. It is deleting selected items from a list.

`#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
}

This function follows the same design pattern. Pipe file output from Get-ChildItem to Read-SpectreMultiSelection. The function will pipe any select files to Remove-Item. The function has a timeout of 15 seconds. If you don't make a selection in that time, the function will exit without deleting anything. I also added support for -WhatIf and -Confirm by adding [CmdletBinding(SupportsShouldProcess)]. This will be passed to Remove-Item.

Select files to delete
figure 1

The list "disappears" after you make your selection.

PS C:\> dir c:\temp\*.ps1 | Select-ToDelete -WhatIf
This prompt times out in 15 seconds...

What if: Performing the operation "Remove File" on target "C:\temp\Blog-ScanningNotes.ps1".
What if: Performing the operation "Remove File" on target "C:\temp\Demo-Information.ps1".
What if: Performing the operation "Remove File" on target "C:\temp\Demo-PowerShellHTML.ps1".
Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.