#Requires -version 3 Function Select-PropertiesForm{ <# .SYNOPSIS The Select-PropertiesForm function lets you select properties to continue down the pipeline .DESCRIPTION Select-PropertiesForm creates a form to select properties from an object and pass them down the pipline as a selected version of the original object. It supports multi-select. This allows the user to select which properties go into an exported list, and in what order. .PARAMETER InputObject This is the object passed as input to the form. .PARAMETER FormTitle The title of the form. Default is: "Select in order the properties to report, then accept list. .PARAMETER ExitOnCancel Stop when form is cancelled. Default is to continue with original object down pipeline. .EXAMPLE Choose properties to view from Get-Process, seeing list in GridView Output TypeName: Selected.System.Diagnostics.Process Get-Process | Select-PropertiesForm | out-gridview .EXAMPLE Choose properties from Get-User, using my Convert-ADValues to ensure that dates and other values export okay, send results to CSV file. Output Typename: Selected.Microsoft.ActiveDirectory.Management.ADUser Get-aduser -Filter ('sn -eq "Smith"') -properties * | Select-PropertiesForm | Convert-ADValues | Export-CSV -NoTypeInformation "$env:userprofile\desktop\Smiths.csv" .INPUTS Any .NET object .OUTPUTS Selected.Object .NOTES Alan dot Kaplan at VA dot Gov 10/27/2014, 5-14-2016 Version 2 added pipeline support, help Version 2.01 fixed typo in help example .LINK www.akaplan.com/blog #> [CmdletBinding()] Param ( # InputObject help description [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [Object[]]$InputObject, # FormTitle help description [Parameter(Mandatory=$False, Position=1)] [string]$FormTitle="Select in order the properties to report, then accept list", # When True, Exit if cancel or close without selecting any properties. Default is continue [Parameter(Mandatory=$False, Position=2)] [Switch]$ExitOnCancel=$False ) Begin{$FirstRun=$True} Process{ if ($firstRun){ #only build form to select properties for first member of pipeline $objProps = ($InputObject| gm -MemberType *property).Name |sort #Initial form code generated by SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0 Add-Type -assemblyname System.Windows.Forms Add-Type -assemblyname System.Drawing $Script:NewList ="" $SelectForm = New-Object System.Windows.Forms.Form $CancelBtn = New-Object System.Windows.Forms.Button $AcceptBtn = New-Object System.Windows.Forms.Button $MoveLftBtn = New-Object System.Windows.Forms.Button $MoveRtBtn = New-Object System.Windows.Forms.Button $lb_NewList = New-Object System.Windows.Forms.ListBox $lb_OrigList = New-Object System.Windows.Forms.ListBox $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState #Event Script Blocks $On_AcceptBtn_Click= { $Script:NewList = @() foreach ($item in $lb_NewList.Items){$Script:NewList += $item} $SelectForm.Close() } # Begin by loading data passed to function $On_SelectForm_Load= { foreach($item in $objProps) {$lb_OrigList.items.add($item)} } #Click the move left button removes from New list and puts back item to the original list $On_MoveLftBtn_Click= { $movelist = @() foreach ($item in $lb_NewList.SelectedItems){$movelist += $item} foreach($item in $movelist){ $lb_NewList.items.remove($item) $lb_OrigList.items.add($item) } } #Click the move right button removes from original list and puts item to the new list $On_MoveRtBtn_Click= { $movelist = @() foreach ($item in $lb_OrigList.SelectedItems){$movelist += $item} foreach($item in $movelist){ $lb_OrigList.items.remove($item) $lb_NewList.items.add($item) } } $OnLoadForm_StateCorrection= {#Sapien says this corrects the initial state of the form to prevent the .Net maximized form issue $SelectForm.WindowState = $InitialFormWindowState } $SelectForm.CancelButton = $CancelBtn $SelectForm.AutoSize = $True $SelectForm.StartPosition = "CenterScreen" $SelectForm.Name = "SelectForm" $SelectForm.Text = $FormTitle $SelectForm.add_Load($On_SelectForm_Load) #Cancel Button $CancelBtn.DialogResult = 2 $CancelBtn.Location =New-Object System.Drawing.Point 255,220 $CancelBtn.Name = "CancelBtn" $CancelBtn.Size = New-Object System.Drawing.Size 75, 25 $CancelBtn.TabIndex = 5 $CancelBtn.Text = "Cancel" $CancelBtn.UseVisualStyleBackColor = $True $CancelBtn.add_Click($On_CancelBtn_Click) $SelectForm.Controls.Add($CancelBtn) #Accept Button $AcceptBtn.Location = New-Object System.Drawing.Point 255, 175 $AcceptBtn.Name = "AcceptBtn" $AcceptBtn.Size = New-Object System.Drawing.Size 75,25 $AcceptBtn.TabIndex = 4 $AcceptBtn.Text = "Accept List" $AcceptBtn.UseVisualStyleBackColor = $True $AcceptBtn.add_Click($On_AcceptBtn_Click) $SelectForm.Controls.Add($AcceptBtn) #Move Left Button $MoveLftBtn.Location = New-Object System.Drawing.Point 255, 110 $MoveLftBtn.Name = "MoveLftBtn" $MoveLftBtn.Size = New-Object System.Drawing.Size 75, 45 $MoveLftBtn.TabIndex = 3 $MoveLftBtn.Text = "<< Move" $MoveLftBtn.UseVisualStyleBackColor = $True $MoveLftBtn.add_Click($On_MoveLftBtn_Click) $SelectForm.Controls.Add($MoveLftBtn) #Move Right Button $MoveRtBtn.Enabled = $True $MoveRtBtn.Location = New-Object System.Drawing.Point 255, 40 $MoveRtBtn.Name = "MoveRtBtn" $MoveRtBtn.Size = New-Object System.Drawing.Size 75,45 $MoveRtBtn.TabIndex = 2 $MoveRtBtn.Text = "Move >>" $MoveRtBtn.UseVisualStyleBackColor = $True $MoveRtBtn.add_Click($On_MoveRtBtn_Click) $SelectForm.Controls.Add($MoveRtBtn) #Left Panel original data $lb_OrigList.FormattingEnabled = $True $lb_OrigList.Location = New-Object System.Drawing.Point 10, 5 $lb_OrigList.Name = "listBox1" $lb_OrigList.Size = New-Object System.Drawing.Size 235, 355 $lb_OrigList.TabIndex = 0 $lb_origList.SelectionMode = "multiExtended" $SelectForm.Controls.Add($lb_OrigList) #Right Panel new data $lb_NewList.FormattingEnabled = $True $lb_NewList.Location = New-Object System.Drawing.Point 345,5 $lb_NewList.Name = "listBox2" $lb_NewList.Size = New-Object System.Drawing.Size 235, 355 $lb_NewList.TabIndex = 1 $lb_NewList.SelectionMode = "multiExtended" $SelectForm.Controls.Add($lb_NewList) #Sapien says save the initial state of the form $InitialFormWindowState = $SelectForm.WindowState #Init the OnLoad event to correct the initial state of the form $SelectForm.add_Load($OnLoadForm_StateCorrection) #Show the Form $SelectForm.ShowDialog()| Out-Null $firstRun = $False } #If they choose nothing or close, return all if ($Script:NewList[0].Count -eq 0){ If ($ExitOnCancel) {Break}ELSE{$InputObject} }ELSE{ $InputObject | select $Script:NewList } } End{} } #End Function