Anany Shah
Anany Shah

Reputation: 27

Change output object's properties of powershell cmdlet based on input parameter set

I have created a PowerShell cmdlet Get-MyName It has 2 parameter set

Parameter Set 1

Get-MyName(No parameter)

Output :-

First: John

Parameter Set 2

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

Answers (1)

user6811411
user6811411

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

Related Questions