Select-FromGridView – Tinkering with Powershell V3

From a 2012 SG entry – a quick and easy way to present a menu of objects to select for processing.  Uses the out-gridview -outputmode option to present a gridview of selected properties of the objects.  What is displayed in the gridview is for display purposes only.  The original objects will be returned.


function Select-FromGridView {
            <#
            .Synopsis
               Selects objects from an array using a gridview of selected properties.
            .DESCRIPTION
              Offers a gridview to select one or more objects from a an array, 
              by a list of selected properties. Input objects will be added to an
              array, and a display object created.  The display object will have an IDX
              property that is the original object index position in the array, and the
              properties specified in the -By parameter taken from the original object.
              The display objects will appear in the gridview. After the selection is made, 
              the IDX property from the selected display object(s) will be used to select the
               original objects from the array to return - i.e. the return will be the original 
               objects, with all properties and methods intact, not the display objects. 
            .EXAMPLE
              $users | select-fromgridview -by * -single | unlock-adaccount
            .EXAMPLE
              get-childitem *.csv | select-fromgridview -by name,lastwritetime -single | invoke-item
            .EXAMPLE
              get-childitem *.txt |
               select-fromgridview -by name,length,creationtime -Title 'Choose files to open'|
                invoke-item
            #>

            #Requires -Version 3

            Param
            (

             # Objects to select from
             [Parameter(ValueFromPipeline=$true)]

             [object[]]
             $InputObjects,
            
             # Fields to display in gridview
             [Parameter(Mandatory=$true,
                     Position=0)]

             [string[]]
             $By,

             # Title to display on gridview
             [String]
             $Title,

             # Select single object
             [Switch]
             $Single
         )

           Begin

           {
             If ($Single){
            
                   $OutputMode = 'Single'

                   }

             Else {

                   $outputMode = 'Multi'

                   }

           If (-not $Title){

                   If ($Single){
     
                                 $Title = 'Choose one.'

                                }

                     Else {

                                 $Title = 'Choose one or more'

                          }

                }


           $gridview = @{
                          Title = $Title
                          OutputMode = $OutputMode
                        }

           $IDX = 0

           $Display = @()

           $InputArray = @()

           $By = @('IDX') + $By

      
                       
         }

    
         Process

         {

            $InputArray += $_
      
            $DisplayItem =   $_ | Select-Object $By

            $DisplayItem.IDX = $IDX++

            $Display += $DisplayItem
                        
         }

        End 

        { 

         $Selected = $Display | Out-Gridview @GridView
             
           Foreach ($Selection in $Selected){

             $InputArray[$($selection.IDX)]

             }
       }
    }

Advertisements

2 responses to “Select-FromGridView – Tinkering with Powershell V3

  1. Awesome indeed, dude! I made one very small change, I added a default of “*” to the “-by” parameter. So if I did not set that parameter, it would just show all properties by default. I had to remove the mandatory and add the default, as noted here:

    # Fields to display in gridview
    [Parameter(Position=0)]
    [string[]]
    $By = “*”,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s