Disassemble Powershell Commandlets

Published Aug 05 2021 12:30 AM 2,528 Views
Microsoft

 

Hey community,

 

I am Helmut Wagensonner, a Customer Engineer at Microsoft and with today’s blog I show you, how to look behind the implementation of Powershell CMDLets.

 

Ever wondered how the Microsoft guys code their powershell Commands? Ever wanted to take a peek at the implementation of a Powershell command? “You take the red pill - you stay in Wonderland, and I show you how deep the rabbit hole goes. Remember: all I'm offering is the truth. Nothing more.” (Matrix)

 

Everything starts with the Get-Command CMDlet.

 

Using Get-Command you can figure out a lot of properties of the command you want to look behind. The most important thing we need to know about a command is its type. The way to get to the script code is different for each command type. There are a lot of command types (https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.commandtypes?view=powershel...) but most Powershell commands are of type “CMDLet” or “Function”. Another command type is “Alias”. But this is just a refenrence to a “CMDLet” or “Function”. So let’s focus on these two for now.

 

For this walkthrough I picked the “GC” (Get-Content) command as example for a CMDLet and “Get-WindowsUpdateLog” as an example for a Function.

 

The CMDLet inside a .NET binary

 

Let’s start to x-ray “Get-Content” by opening a Powershell window in administrative mode and typing the following command:

 

 

Get-Command gc | fl *

 

 

DisassemblePoSh01.png

 

If you don’t elevate the Powershell window only a subset of the properties are shown.


In the Get-Command example above I’m using the short form “GC” instead of “Get-Content” as an example. This shows “Alias” as CommandType property. To figure out the full name of the command, take a look at the “ResolvedCommand” property and repeat Get-Command with its value. Using the long form shows the real command type, in this case a CMDLet.

 

 

Get-Command Get-Content | fl *

 

 

DisassemblePoSh02.png

 

The DLL property shows the implementation source of this CMDLet. Since this is a binary format (.DLL), you need a disassembler to continue. There are a lot of tools to disassemble a dotNet binaries. I use ILSpy. It’s fast and can be installed as a portable app (no installation necessary).

 

So let’s open the file from the DLL property (Microsoft.PowerShell.Commands.Management.dll) in ILSpy. Most times the command implementation is found in the “Microsoft.PowerShell.Commands” namespace.

 

DisassemblePoSh03.png


Typically the command classes are named similar to the PowerShell command. In our case you find the implementation in the GetContentCommand class. Sometimes you need to dig a bit deeper but I’m quite sure you will find what you’re looking for.

 

From now on little knowledge about .NET programming is necessary but as a rough indication you can search for methods like “ProcessRecord” or “BeginProcessing” to get a starting point.

 

The Function way

 

It’s different when the source code for a command is implemented as a function. As mentioned at the beginning, I chose the “Get-WindowsUpdateLog” command as an example here. So let’s have a look at its members.

 

 

Get-Command Get-WindowsUpdateLog | fl *

 

 

DisassemblePoSh05.png


I shortened the output. In your case the ScriptBlock and Definition properties should be much larger. The source code of the function itself can be found in the “Definition” property. So it can easily be displayed using the command:

 

 

Get-Command Get-WindowsupdateLog | select -ExpandProperty Definition

 

 

DisassemblePoSh06.png


However, in many cases, the function is using code parts or other functions from its parent module. To view the complete source code, you need to open the whole module in a text editor.


First, let’s find the parent module:

 

 

Get-Command Get-WindowsUpdateLog | select ModuleName

 

 

DisassemblePoSh07.png


It’s part of the “WindowsUpdate” module. But where to find it? Well, that’s easy:

 

 

Get-Module -Name WindowsUpdate | select Path

 

 

DisassemblePoSh08.png


This displays the path to the module definition file (*.psd1) or to the compiled module (*.dll) The compiled module can be opened with your favorite decompiler. In this example we have a module definition file. So let’s open the PSD1 in a text editor.

 

DisassemblePoSh09.png


Find the line “ModuleList” or “NestedModules”. In both of them you can figure out the path to the module files (*.psm1).


The module files normally are located in the same directory. If not you will find a full path name after the ModuleList or NestedModules values. One of those files contains the implementation of the function, found in the definition property of the Get-Command output. In our case it’s easy because this psd1 only contains one module. So let’s go and open the PSM1 file in your favorite text editor.

 

DisassemblePoSh10.png

 

Here we have the complete implementation of our function.


You may have noticed, that I used the terms “normally” and “most times…” a few times in this blog post. This is because there are different ways for implementation of CMDLets. Disassembling other commands could slightly vary from the steps mentioned above.

 

Anyway this gives you a starting point and it works for most CMDLet implementations coming from Microsoft.

 

 

 

Disclaimer
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.

%3CLINGO-SUB%20id%3D%22lingo-sub-2603180%22%20slang%3D%22en-US%22%3EDisassemble%20Powershell%20Commandlets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2603180%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHey%20community%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20Helmut%20Wagensonner%2C%20a%20Customer%20Engineer%20at%20Microsoft%20and%20with%20today%E2%80%99s%20blog%20I%20show%20you%2C%20how%20to%20look%20behind%20the%20implementation%20of%20Powershell%20CMDLets.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EEver%20wondered%20how%20the%20Microsoft%20guys%20code%20their%20powershell%20Commands%3F%20Ever%20wanted%20to%20take%20a%20peek%20at%20the%20implementation%20of%20a%20Powershell%20command%3F%20%E2%80%9CYou%20take%20the%20red%20pill%20-%20you%20stay%20in%20Wonderland%2C%20and%20I%20show%20you%20how%20deep%20the%20rabbit%20hole%20goes.%20Remember%3A%20all%20I'm%20offering%20is%20the%20truth.%20Nothing%20more.%E2%80%9D%20(Matrix)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EEverything%20starts%20with%20the%20Get-Command%20CMDlet.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUsing%20Get-Command%20you%20can%20figure%20out%20a%20lot%20of%20properties%20of%20the%20command%20you%20want%20to%20look%20behind.%20The%20most%20important%20thing%20we%20need%20to%20know%20about%20a%20command%20is%20its%20type.%20The%20way%20to%20get%20to%20the%20script%20code%20is%20different%20for%20each%20command%20type.%20There%20are%20a%20lot%20of%20command%20types%20(%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.management.automation.commandtypes%3Fview%3Dpowershellsdk-1.1.0%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.management.automation.commandtypes%3Fview%3Dpowershellsdk-1.1.0%3C%2FA%3E)%20but%20most%20Powershell%20commands%20are%20of%20type%20%E2%80%9CCMDLet%E2%80%9D%20or%20%E2%80%9CFunction%E2%80%9D.%20Another%20command%20type%20is%20%E2%80%9CAlias%E2%80%9D.%20But%20this%20is%20just%20a%20refenrence%20to%20a%20%E2%80%9CCMDLet%E2%80%9D%20or%20%E2%80%9CFunction%E2%80%9D.%20So%20let%E2%80%99s%20focus%20on%20these%20two%20for%20now.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20this%20walkthrough%20I%20picked%20the%20%E2%80%9CGC%E2%80%9D%20(Get-Content)%20command%20as%20example%20for%20a%20CMDLet%20and%20%E2%80%9CGet-WindowsUpdateLog%E2%80%9D%20as%20an%20example%20for%20a%20Function.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3EThe%20CMDLet%20inside%20a%20.NET%20binary%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELet%E2%80%99s%20start%20to%20x-ray%20%E2%80%9CGet-Content%E2%80%9D%20by%20opening%20a%20Powershell%20window%20in%20administrative%20mode%20and%20typing%20the%20following%20command%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3EGet-Command%20gc%20%7C%20fl%20*%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20don%E2%80%99t%20elevate%20the%20Powershell%20window%20only%20a%20subset%20of%20the%20properties%20are%20shown.%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EIn%20the%20Get-Command%20example%20above%20I%E2%80%99m%20using%20the%20short%20form%20%E2%80%9CGC%E2%80%9D%20instead%20of%20%E2%80%9CGet-Content%E2%80%9D%20as%20an%20example.%20This%20shows%20%E2%80%9CAlias%E2%80%9D%20as%20CommandType%20property.%20To%20figure%20out%20the%20full%20name%20of%20the%20command%2C%20take%20a%20look%20at%20the%20%E2%80%9CResolvedCommand%E2%80%9D%20property%20and%20repeat%20Get-Command%20with%20its%20value.%20Using%20the%20long%20form%20shows%20the%20real%20command%20type%2C%20in%20this%20case%20a%20CMDLet.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3EGet-Command%20Get-Content%20%7C%20fl%20*%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20DLL%20property%20shows%20the%20implementation%20source%20of%20this%20CMDLet.%20Since%20this%20is%20a%20binary%20format%20(.DLL)%2C%20you%20need%20a%20disassembler%20to%20continue.%20There%20are%20a%20lot%20of%20tools%20to%20disassemble%20a%20dotNet%20binaries.%20I%20use%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ficsharpcode%2FILSpy%2Freleases%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3EILSpy%3C%2FA%3E.%20It%E2%80%99s%20fast%20and%20can%20be%20installed%20as%20a%20portable%20app%20(no%20installation%20necessary).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20let%E2%80%99s%20open%20the%20file%20from%20the%20DLL%20property%20(Microsoft.PowerShell.Commands.Management.dll)%20in%20ILSpy.%20Most%20times%20the%20command%20implementation%20is%20found%20in%20the%20%E2%80%9CMicrosoft.PowerShell.Commands%E2%80%9D%20namespace.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3ETypically%20the%20command%20classes%20are%20named%20similar%20to%20the%20PowerShell%20command.%20In%20our%20case%20you%20find%20the%20implementation%20in%20the%20GetContentCommand%20class.%20Sometimes%20you%20need%20to%20dig%20a%20bit%20deeper%20but%20I%E2%80%99m%20quite%20sure%20you%20will%20find%20what%20you%E2%80%99re%20looking%20for.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFrom%20now%20on%20little%20knowledge%20about%20.NET%20programming%20is%20necessary%20but%20as%20a%20rough%20indication%20you%20can%20search%20for%20methods%20like%20%E2%80%9CProcessRecord%E2%80%9D%20or%20%E2%80%9CBeginProcessing%E2%80%9D%20to%20get%20a%20starting%20point.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3EThe%20Function%20way%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%E2%80%99s%20different%20when%20the%20source%20code%20for%20a%20command%20is%20implemented%20as%20a%20function.%20As%20mentioned%20at%20the%20beginning%2C%20I%20chose%20the%20%E2%80%9CGet-WindowsUpdateLog%E2%80%9D%20command%20as%20an%20example%20here.%20So%20let%E2%80%99s%20have%20a%20look%20at%20its%20members.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3EGet-Command%20Get-WindowsUpdateLog%20%7C%20fl%20*%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EI%20shortened%20the%20output.%20In%20your%20case%20the%20ScriptBlock%20and%20Definition%20properties%20should%20be%20much%20larger.%20The%20source%20code%20of%20the%20function%20itself%20can%20be%20found%20in%20the%20%E2%80%9CDefinition%E2%80%9D%20property.%20So%20it%20can%20easily%20be%20displayed%20using%20the%20command%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3EGet-Command%20Get-WindowsupdateLog%20%7C%20select%20-ExpandProperty%20Definition%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EHowever%2C%20in%20many%20cases%2C%20the%20function%20is%20using%20code%20parts%20or%20other%20functions%20from%20its%20parent%20module.%20To%20view%20the%20complete%20source%20code%2C%20you%20need%20to%20open%20the%20whole%20module%20in%20a%20text%20editor.%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EFirst%2C%20let%E2%80%99s%20find%20the%20parent%20module%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3EGet-Command%20Get-WindowsUpdateLog%20%7C%20select%20ModuleName%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EIt%E2%80%99s%20part%20of%20the%20%E2%80%9CWindowsUpdate%E2%80%9D%20module.%20But%20where%20to%20find%20it%3F%20Well%2C%20that%E2%80%99s%20easy%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3EGet-Module%20-Name%20WindowsUpdate%20%7C%20select%20Path%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EThis%20displays%20the%20path%20to%20the%20module%20definition%20file%20(*.psd1)%20or%20to%20the%20compiled%20module%20(*.dll)%20The%20compiled%20module%20can%20be%20opened%20with%20your%20favorite%20decompiler.%20In%20this%20example%20we%20have%20a%20module%20definition%20file.%20So%20let%E2%80%99s%20open%20the%20PSD1%20in%20a%20text%20editor.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EFind%20the%20line%20%E2%80%9CModuleList%E2%80%9D%20or%20%E2%80%9CNestedModules%E2%80%9D.%20In%20both%20of%20them%20you%20can%20figure%20out%20the%20path%20to%20the%20module%20files%20(*.psm1).%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EThe%20module%20files%20normally%20are%20located%20in%20the%20same%20directory.%20If%20not%20you%20will%20find%20a%20full%20path%20name%20after%20the%20ModuleList%20or%20NestedModules%20values.%20One%20of%20those%20files%20contains%20the%20implementation%20of%20the%20function%2C%20found%20in%20the%20definition%20property%20of%20the%20Get-Command%20output.%20In%20our%20case%20it%E2%80%99s%20easy%20because%20this%20psd1%20only%20contains%20one%20module.%20So%20let%E2%80%99s%20go%20and%20open%20the%20PSM1%20file%20in%20your%20favorite%20text%20editor.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20we%20have%20the%20complete%20implementation%20of%20our%20function.%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EYou%20may%20have%20noticed%2C%20that%20I%20used%20the%20terms%20%E2%80%9Cnormally%E2%80%9D%20and%20%E2%80%9Cmost%20times%E2%80%A6%E2%80%9D%20a%20few%20times%20in%20this%20blog%20post.%20This%20is%20because%20there%20are%20different%20ways%20for%20implementation%20of%20CMDLets.%20Disassembling%20other%20commands%20could%20slightly%20vary%20from%20the%20steps%20mentioned%20above.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnyway%20this%20gives%20you%20a%20starting%20point%20and%20it%20works%20for%20most%20CMDLet%20implementations%20coming%20from%20Microsoft.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDisclaimer%3CBR%20%2F%3EThe%20sample%20scripts%20are%20not%20supported%20under%20any%20Microsoft%20standard%20support%20program%20or%20service.%20The%20sample%20scripts%20are%20provided%20AS%20IS%20without%20warranty%20of%20any%20kind.%20Microsoft%20further%20disclaims%20all%20implied%20warranties%20including%2C%20without%20limitation%2C%20any%20implied%20warranties%20of%20merchantability%20or%20of%20fitness%20for%20a%20particular%20purpose.%20The%20entire%20risk%20arising%20out%20of%20the%20use%20or%20performance%20of%20the%20sample%20scripts%20and%20documentation%20remains%20with%20you.%20In%20no%20event%20shall%20Microsoft%2C%20its%20authors%2C%20or%20anyone%20else%20involved%20in%20the%20creation%2C%20production%2C%20or%20delivery%20of%20the%20scripts%20be%20liable%20for%20any%20damages%20whatsoever%20(including%2C%20without%20limitation%2C%20damages%20for%20loss%20of%20business%20profits%2C%20business%20interruption%2C%20loss%20of%20business%20information%2C%20or%20other%20pecuniary%20loss)%20arising%20out%20of%20the%20use%20of%20or%20inability%20to%20use%20the%20sample%20scripts%20or%20documentation%2C%20even%20if%20Microsoft%20has%20been%20advised%20of%20the%20possibility%20of%20such%20damages.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2603180%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20article%20is%20about%20disassembling%20Powershell%20CMDLets.%20I%20shows%20you%20an%20easy%20way%20to%20view%20the%20implementation%20of%20the%20commands.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2603180%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EHelmutWagensonner%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Aug 05 2021 08:03 AM
Updated by: