How to hide parameters under | fl * -FORCE

Hi,
could someone help me to write a program where I should have specified parameters should get displayed only when user uses " verb-noun | fl * -force " syntax. I tried hard to at least to know how it can be implemented by googling and by my own experiments but I am completely in vain. As per my guess, it can be done by .psmxml1 file; if so I don’t know how I should write it. Any example of code is very helpful to me.

The below is the ideal kind of implementation that I am looking for where -fore displays the hidden/non priority properties like PSObject, PS* …etc

Get-Service dns* | gm -force

Thanks in advance.

You can do this if you’re playing with classes in the PowerShell v5 preview, by using the hidden keyword on your properties, constructors, etc:

class Test
{
    [int] $A

    hidden [int] $B

    Test($a, $b)
    {
        $this.A = $a
        $this.B = $b
    }
}

$test = [Test]::new(5, 4)


Write-Verbose -Verbose 'Without -Force'
$test | Get-Member | Out-Host

Write-Verbose -Verbose 'With -Force'
$test | Get-Member -Force | Out-Host

For other .NET objects, I don’t know of a public way of doing this. There is an “IsHidden” property on PSMemberInfo objects, but it’s internal. You can technically modify that if you don’t mind using Reflection, and accept that the code might break in future versions of PowerShell.

Apologies if I am answering the wrong question but I’m not sure whether you want to actually have hidden properties and methods or whether you’re just looking to return a subset of properties from your script. The latter can be achieved using the Extensible Type System (ETS). You define a type extension file which defines which properties to display by default and you then use the file in your script. This is covered in some detail in PowerShell in Depth (chapter 27) but I have simplified the concept below:

First, create a type extension file. I have saved this file as F:__Temp\MyTypes.ps1xml
This simple demonstration file just defines two properties to be displayed by default: Forename and Surname.

Edit: Couldn’t get the XML to display properly so I have attached a screenshot.

Next I created a test script that just creates a simple “Person” object. The object has seven properties.
Lines 3 and 18 show how the type extension is used in the script.

function New-TestObject {

    Update-TypeData -AppendPath F:\__Temp\MyTypes.ps1xml

    $properties = @{
        
        Forename     = "Bob"
        Surname      = "Upandown"
        Address      = "99 Any Street"
        Town         = "Sometown"
        County       = "Anyshire"
        Country      = "Democratic Republic of PowerShell"
        Phone        = "01234568"

    }

    $obj = New-Object -TypeName PSObject -Property $properties
    $obj.PSObject.TypeNames.Insert(0,"MyTypes.Person")
    Write-Output $obj


}
 

If you run New-TestObject on its own it will output just the Forename and Surname.

If you run New-TestObject | Format-List * all seven properties will be displayed.

Thanks Matt and Dave.
Matt: First of all thnx very much for taking time to explain me and giving the chapter number info of PowerShell In Depth. What you have given me taught me how to write .PS1XML for Format-List, but actually it won’t server my requirement. May be what Dave said is correct.

Dave: Thanks a lot Dave. The sample code is perfect example for me. Now i should go back to my college days to recall Classes, Objects …etc.