Forwarding Selected Parameters with PowerShell

by Sep 20, 2012

In a previous tip you learned how you can forward function parameters to cmdlets. But what if you just want to forward some of the parameters? Here is a sample:

function Get-BIOS($ComputerName, $Credential, [switch]$Verbose) {
  $a = $global:psboundparameters
  $psboundparameters.remove('verbose') | Out-Null
  $bios = Get-WmiObject Win32_BIOS @psboundparameters
  if ($verbose) { $bios | Select-Object * } else { $bios }
}

Get-BIOS -Verbose
Get-BIOS -ComputerName storage1 -Verbose

The function supports three parameters, but only two should be forwarded to Get-WmiObject. The remaining parameter -Verbose is used internally by the function. To prevent -Verbose from being forwarded to Get-WmiObject, you can remove that key from $psboundparameters before you splat it to Get-WmiObject.

Twitter This Tip! ReTweet this Tip!