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!
Identifying PowerShell functions
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.
Get-Command Pause
Since we verified that it’s a function, let’s see what the function Pause is actually doing under the hood.
Viewing function contents
Here are 3 different ways to view function contents. You can pick and choose which method works best for you.
Method 1 – Using Get-Command
Get-Command <Function Name> | Select -ExpandProperty ScriptBlockOr
(Get-Command <Function Name>).ScriptBlockTa da! That’s it!
Get-Command Pause | Select -ExpandProperty ScriptBlockSo, it looks like the function Pause runs the following command:
$null = Read-Host 'Press Enter to continue…'How fancy!
Method 2 – Using $function
Another fancy way to see the contents of the pause function is to use the following:
$Function:PauseSo simple, so wonderful.
Method 3 – Using the Function: Drive
Functions are automatically stored in the Function: Drive. You can see this yourself by typing:
Get-PSDriveSince 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.
Showing all available Functions
In order to see all available functions, you could run this command. You will see a lot of results.
Get-Command -CommandType Function
Wrapping Up
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).
Join me every 1st and 3rd Tuesday of the month at 10:00 AM, MDT for live PowerShell demonstrations!
Have fun and happy PowerShelling!
Too see this in action, please enjoy this short video: