Reputation: 27
I have created a PowerShell cmdlet
Get-MyName
It has 2 parameter set
Get-MyName
(No parameter)
Output :-
First: John
Get-MyName -Full $true
Output :-
First :John
Last :Gardener
I want the cmdlet to return same object for both the parameter set. Only difference is that in First parameter set the property Last
should not be present.
Is there any way I can do it?
Upvotes: 0
Views: 126
Reputation:
All you need to do in your script/function is to return the object
with different properties depending on your switch -Full
function Get-MyName {
param(
[switch]$Full
)
$Object = [PSCustomObject]@{First='John';Last='Gardener'}
if($Full){
return $Object
} else {
return ($Object | Select-Object -Property * -ExcludeProperty Last)
}
}
Sample output:
PoSh> Get-MyName
First
-----
John
PoSh> Get-MyName -Full
First Last
----- ----
John Gardener
Upvotes: 1