Does this site look plain?

This site uses advanced css techniques

This Cmdletter expands on a previous blog post.

When building a new system, I typically prototype it in PowerShell, resorting quickly and casually to cmdlets to support the proof of concept, and this invariably involves .NET objects that get passed around in the pipeline, and sometimes presented to the user (usually me) via Format-Table or Out-GridView.

By default, all public fields and properties are shown to the user, but there are often times when one or more field/property should not be shown, mainly just to keep the output from being clutter with internal values that are nevertheless necessary.

In this contrived example, we have an Employee class, where the user-important values are the employee's name and their employee number. This employee number is not the same as the internal employee ID (perhaps it's the primary key in the database), and though it's necessary for later cmdlets that consume the object, the user doesn't really need to see it.

Tidbit: in the payroll industry, "EE" is a common abbreviation for "employee", while "ER" is the abbreviation for "Employer".

using System.Management.Automation;  // required for HiddenAttribute

...

    public class Employee
    {
        [Hidden]
        public int EEID { get; set; }  // internal employee ID

        public string EmployeeNumber { get; set; } // public EE number
        public string FirstName      { get; set; }
        public string LastName       { get; set; }
        //.. more
    }

When this object is displayed to the user in any fashion, the EEID won't be shown.

Bad Idea: this is not a security mechanism! The hidden values are still trivially available as a public property (e.g., $ee.EEID), and discoverable by Get-Member, so this is strictly about convenience, not security.

This attribute appears to have been added in Windows Management Framework 5.1 (aka PowerShell 5.1), which is available in Server 2008 R2 and above; in lower versions of PowerShell, the [Hidden] attribute is quietly ignored and all object items are shown.

Links


The excellent icons on this page were made by Freepik from www.flaticon.com, and are licensed by CC 3.0 BY