Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Powershell's Format-List * not output all properties

Tags:

powershell

When I do a ... | Format-List one of the properties is Path.
When I do a ... | Format-List * the Path property is missing.
Why?

[DBG]: PS C:\Directory>> $MyInvocation.MyCommand | Format-List

Name        : 
CommandType : Script
Definition  : $MyInvocation.MyCommand | Format-List
Path        : 

[DBG]: PS C:\Directory>> $MyInvocation.MyCommand | Format-List *

HelpUri            : 
ScriptBlock        : $MyInvocation.MyCommand | Format-List *
Definition         : $MyInvocation.MyCommand | Format-List *
OutputType         : {}
Name               : 
CommandType        : Script
Visibility         : Public
ModuleName         : 
Module             : 
RemotingCapability : PowerShell
Parameters         : 
ParameterSets      : {}
like image 937
LosManos Avatar asked Mar 08 '23 14:03

LosManos


2 Answers

To compliment gms0ulman's answer...

PowerShell displays objects to console based on format.ps1xml files. From Formatting File Overview

The display format for the objects that are returned by commands (cmdlets, functions, and scripts) are defined by using formatting files (format.ps1xml files). Several of these files are provided by Windows PowerShell to define the display format for those objects returned by Windows PowerShell–provided commands ...

PowerShell, when instructed, has a specific way of displaying objects when not told otherwise. In your case this comes from the file PowerShellCore.format.ps1xml. From checking the type of $MyInvocation.MyCommand we know that it is a System.Management.Automation.ScriptInfo object. Knowing that we can pull the format details from the above file.

<View>
    <Name>System.Management.Automation.ScriptInfo</Name>
    <ViewSelectedBy>
        <TypeName>System.Management.Automation.ScriptInfo</TypeName>
    </ViewSelectedBy>


    <ListControl>
        <ListEntries>
            <ListEntry>
               <ListItems>
                    <ListItem>
                        <PropertyName>Name</PropertyName>
                    </ListItem>
                    <ListItem>
                        <PropertyName>CommandType</PropertyName>
                    </ListItem>
                    <ListItem>
                        <PropertyName>Definition</PropertyName>
                    </ListItem>
                    <ListItem>
                        <PropertyName>Path</PropertyName>
                    </ListItem>
                </ListItems>
            </ListEntry>
        </ListEntries>
    </ListControl>
</View>

So we see that it will try to display Name, CommandType, Definition and Path. As you know specifying * for all properties will show all present properties for an object1

Path is null of course because of where you ran it from.

1 Minor exception to that statement in that some common object properties are hidden to *. Use -Force to show all in that case. They are accessible regardless if you know their name.

like image 191
Matt Avatar answered Mar 10 '23 05:03

Matt


When running in the console, the Path property doesn't actually exist. Format-List - without * - is using a template to determine which properties to display. Unfortunately I can't find documentation links to explain this further, and how this can be configured - will edit if I do.
See Matt's answer

You can see this by running $MyInvocation.MyCommand | Get-Member and noting that Path is not one of the properties. Makes sense, as you are running the command in the console, not from a script.

In a script, both $MyInvocation.MyCommand | Format-List and $MyInvocation.MyCommand | Format-List * include Path


$MyInvocation is populated only for scripts, function, and script blocks.

From about_Automatic_Variables. Looks like PowerShell is treating your command as a script in order for $MyInvocation to be (partly) populated.

like image 37
G42 Avatar answered Mar 10 '23 03:03

G42