Exploring Function Source Code

by Nov 17, 2016

The only fundamental difference between cmdlets and functions in PowerShell is the way how they are programmed: functions use plain PowerShell code, which is why it might be interesting to look at their source code, and learn new things.

Here is a line that lists all PowerShell functions that are currently loaded from modules:

Get-Module | ForEach-Object { Get-Command -Module $_.Name -CommandType Function }

Once you know the name of a function in memory, here is a quick way to view its source code. In these examples, we’ll explore the inside of the Format-Hex function. Just replace this name by the name of any other function that exists in memory:

${function:Format-Hex} | clip.exe

The line places the source code into the clipboard, and you can paste it from there into the editor of your choice. As an alternative, you could also run this:

Get-Command -Name Format-Hex -CommandType Function |
  Select-Object -ExpandProperty Definition |

Twitter This Tip! ReTweet this Tip!