#PSTip Another way to get a function definiton

In the previous tip, we’ve showed you how to get a function definition using cool and unusual technique. Can we use the Get-Command cmdlet to get the same result? Let’s look at properties of a FunctionInfo object returned by Get-Command prompt command:

PS> Get-Command prompt | Get-Member
   TypeName: System.Management.Automation.FunctionInfo
Name                MemberType     Definition
----                ----------     ----------
Equals              Method         bool Equals(System.Object obj)
GetHashCode         Method         int GetHashCode()
GetType             Method         type GetType()
ResolveParameter    Method         System.Management.Automation.ParameterMetadata ResolveParameter(string name)
ToString            Method         string ToString()
PSDrive             NoteProperty   System.Management.Automation.PSDriveInfo PSDrive=Function
PSIsContainer       NoteProperty   System.Boolean PSIsContainer=False
PSPath              NoteProperty   System.String PSPath=Microsoft.PowerShell.Core\Function::prompt
PSProvider          NoteProperty   System.Management.Automation.ProviderInfo PSProvider=Microsoft.PowerShell.Core\Fu...
Capability          Property       System.Management.Automation.CommandCapability Capability {get;}
CmdletBinding       Property       bool CmdletBinding {get;}
CommandType         Property       System.Management.Automation.CommandTypes CommandType {get;}
Data                Property       System.Object Data {get;set;}
DefaultParameterSet Property       string DefaultParameterSet {get;}
Definition          Property       string Definition {get;}
Description         Property       string Description {get;set;}
HelpFile            Property       string HelpFile {get;}
Module              Property       psmoduleinfo Module {get;}
...

Bingo! The commands in the function are stored as a script block in the Definition property of the function.

PS> Get-Command prompt | Select-Object -Property Definition

Definition
----------
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "...

But wait, that’s not the whole definition! Luckily, the Select-Object has the ExpandProperty parameter. As its name implies, its purpose is to expand the specified property. For example, if the specified property is an array, each value of the array is included in the output. If the property contains an object, the properties of that object are displayed in the output.

PS> Get-Command prompt | Select-Object -ExpandProperty Definition
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
# .Link
# http://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

Some people are fond of pithier syntax using dot notation:

PS> (Get-Command prompt).definition

Note: If you prefer to work with the Function: drive, the following command will return the same object as Get-Command prompt:

PS> Get-ChildItem function:prompt
Share on: