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 : {}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With