SplattingTools.psm1

function New-SplatTable {
  <#
  .SYNOPSIS
    This creates a Splat Table from the existing cmdlet parameters
  .DESCRIPTION
    This command lists all of the parameters for a given command and
    allows you to select which of these will be added to the splat table
  .PARAMETER Cmdlet
    This parameter specifies which command will be used to create the splat table
  .PARAMETER NoClipboard
    By default this command will create a splat table and store it in the
    clipboard so that it can be easily added to other scripts, If you use
    this parameter, the splat table will be sent to standard output
  .PARAMETER IncludeCommonParams
    By default this command will create a splat table and store it in the
    clipboard so that it can be easily added to other scripts, If you use
    this parameter, the option to select from the common parameters will
    also be given, by default this script does not display common parameters
  .NOTES
    Created By: Brent Denny
    Cerated On: 20-Jan-2026
      Change Log
      ----------
      21-Jan-2026 - Added the help content
      21-Jan-2026 - Gave option to include common params, default now skips them
      21-Jan-2026 - Quit the script if cancelled or null selection of params
  .EXAMPLE
    New-SplatTable -Cmdlet New-ADUser
    This will create a list of the parameters available for the New-ADUser command
    and then request that you select which of those get added to the splat table
    it will then create the splat table and add it to the clipboard, where it can be
    easily pasted into a new script.
  .EXAMPLE
    New-SplatTable -Cmdlet New-ADUser -NoClipboard
    This will create a list of the parameters available for the New-ADUser command
    and then request that you select which of those get added to the splat table
    it will then display the splat table on screen
  #>
 
  [CmdletBinding()]
  Param (
    [string]$Cmdlet = 'Get-Service',
    [switch]$NoClipboard,
    [switch]$IncludeCommonParams
  )
  try {$CommandInfo = Get-Command -Name $Cmdlet -ErrorAction stop}
  catch {write-warning "$Cmdlet is not a PowerShell Command"; break}
  $CommonParamList = @('ErrorAction','ErrorVariable','Verbose',
                       'Debug','WarningAction','InformationAction',
                       'WarningVariable','InformationVariable',
                       'OutVariable','OutBuffer','PipelineVariable','ProgressAction')
  $ParameterSets = $CommandInfo.ParameterSets
  # $ParameterSetNames = $ParameterSets.name # this will be for future reference when we can choose which parameter set to use
  # $ParamSetCount = $ParameterSetNames.count # this will be for future reference when we can choose which parameter set to use
  if ($IncludeCommonParams -eq $true) {$Parameters = $ParameterSets.Parameters}
  else {$Parameters = $ParameterSets.Parameters | Where-Object {$_.Name -notin $CommonParamList}}
  $ChosenParamNames = $Parameters.Name | 
    Select-Object -Unique |
    Sort-object |
    Out-GridView -Title 'Select the parameters for the splat table' -OutputMode Multiple
  if (-not $ChosenParamNames) {break}
  $MaxLengthParamName = ($ChosenParamNames | Measure-Object -Property Length -Maximum).Maximum + 1
  if ($NoClipboard -eq $true) { # Send output to screen
    '@{' 
    foreach ($ParameterName in $ChosenParamNames) {
      $SpaceMultipler = $MaxLengthParamName - ($ParameterName.length)
      " " + $ParameterName + (' ' * $SpaceMultipler) + '= " " '
    } #foreach
    '}'
  } #if
  else { # Send output to clipboard
    '@{' | Set-Clipboard 
    foreach ($ParameterName in $ChosenParamNames) {
      $SpaceMultipler = $MaxLengthParamName - ($ParameterName.length)
      " " + $ParameterName + (' ' * $SpaceMultipler) + '= " " ' | Set-Clipboard -Append
    } #foreach
    '}' | Set-Clipboard -Append
    Write-Warning 'The splat table as been stored in the clipboard'
  } #else
} #function