Have you ever thought to yourself, "Self… how does this PowerShell function work?" If you have, then you're in luck! With PowerShell, it's super simple to view Function contents.
In fact, we're going to show you a few different ways to display that info.
So, hold on to your chairs and your monocles! Let's dive right in and find out more!
First, we need to identify a function to peek at.
For this exercise, I have selected the command Pause for its simplicity. Before we proceed, let's verify that this command is truly a function.
Since we verified that it's a function, let's see what the function Pause is actually doing under the hood.
Here are 3 different ways to view function contents. You can pick and choose which method works best for you.
Get-Command <Function Name> | Select -ExpandProperty ScriptBlock
(Get-Command <Function Name>).ScriptBlock
Ta da! That's it!
Get-Command Pause | Select -ExpandProperty ScriptBlock
So, it looks like the function Pause runs the following command:
$null = Read-Host 'Press Enter to continue…'
Another fancy way to see the contents of the pause function is to use the following:
So simple, so wonderful.
Functions are automatically stored in the Function: Drive. You can see this yourself by typing:
Since this is a drive, we can browse it with Get-ChildItem.
Get-ChildItem Function:Pause | Select *
Now we can see the contents of all functions should we so desire.
In order to see all available functions, you could run this command. You will see a lot of results.
Get-Command -CommandType Function
One thing to note is that this will only work for functions. If you want to know what cmdlets are doing, you can go take a look at the PowerShell open source project on GitHub (link here).
Have fun and happy PowerShelling!
Too see this in action, please enjoy this short video: