How to hide parameters under | fl * -FORCE

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of  Anonymous 1 year, 5 months ago.

  • Author
  • #25112
    Profile photo of

    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.

  • #25115
    Profile photo of Dave Wyatt
    Dave Wyatt

    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.

  • #25119
    Profile photo of Matt Bloomfield
    Matt Bloomfield

    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.

    [b]Edit: Couldn't get the XML to display properly so I have attached a screenshot. [/b]

    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
        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.

  • #25125
    Profile photo of

    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.

You must be logged in to reply to this topic.