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.
3 ways to view function contents
Here are 3 different ways to view function contents. You can pick and choose which method works best for you.
1. Using Get-Command
Get-Command
<Function Name> | Select -ExpandProperty ScriptBlock
OR
(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…'
How fancy!
2. Using the $function
Another fancy way to see the contents of the pause function is to use the following:
$Function:Pause
So simple, so wonderful.
3. Using the Function: Drive
Functions are automatically stored in the Function: Drive. You can see this yourself by typing: Get-PSDrive
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.
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).
Too see this in action, please enjoy this short video:
Loading...