Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Monday, October 24, 2016

How to See a Cmdlet’s Code

This last week I had the privilege of speaking at the PowerShell Summit Asia in Singapore.  After an 11 hour day of speaking, you better believe that I had a lot of attendees asking me questions over the following 2 days.  A really good one that stuck in my head involves how to see the code that was used to write a cmdlet.  Hey, I teach PowerShell so this is a good one.

PowerShell cmdlets are actually either a “cmdlet” or a “function”.

PS C:\> Get-Command -Name Get-Process, Get-SMBShare

CommandType     Name            Version    Source                                                                               
-----------     ----            -------    ------                                                                               
Cmdlet          Get-Process     3.1.0.0    Microsoft.PowerShell.Management                                                      
Function        Get-SmbShare    2.0.0.0    smbshare                                                                             


Cmdlets are actually created from C# while functions are created with PowerShell.  Since C# is compiled we cannot crack them open to see what makes them tick without some extra software.  However, you can very easily see the code from a function

PS C:\> Get-Command -Name Get-SMBShare | Select-Object -ExpandProperty ScriptBlock | Out-File -FilePath ##Someplace to put it "

Set the –FilePath to a txt or PS1 file so you can look at the code.


It is no secret that I advocate to practice of “never reinvent the wheel”.  In other words if you need to know how a function did something, just look at the code.  

No comments: