Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we see the source code for PowerShell cmdlets?

People also ask

Are PowerShell cmdlets open source?

Announcing: Open Source PowerShell Cmdlet and Help Designer The Cmdlet Designer makes it much easier for teams to concentrate on the design, naming, and consistency of their cmdlets, while also guaranteeing name registration and collision avoidance across Microsoft.

How do I see cmdlets in PowerShell?

Use CommandType or its alias, Type. By default, Get-Command gets all cmdlets, functions, and aliases. The acceptable values for this parameter are: Alias : Gets the aliases of all PowerShell commands.

Where are PowerShell cmdlets stored?

By default, on Windows 10 and higher, that location is $HOME\Documents\PowerShell\Modules . On Linux or Mac, the CurrentUser location is $HOME/. local/share/powershell/Modules .

What is the PowerShell cmdlet used to display the information shown?

The Show-Command cmdlet lets you create a PowerShell command in a command window. You can use the features of the command window to run the command or have it return the command to you. Show-Command is a very useful teaching and learning tool.


The source for Powershell is now available on Github.
The source for Get-ChildItem can be found here.


Actually, your best bet is to go check out PowerShell Community Extensions. This open source software community project is "aimed at providing a widely useful set of additional cmdlets...". The developers on the project are PowerShell MVPs and know their stuff.

As far as using reflection on the existing PowerShell cmdlets, PowerShell MVP Oisin Grehan made a handy function titled "Reflect-Cmdlet". I won't steal his code and place it here, but basically what you do is:

Get-Command Get-ChildItem | Reflect-Cmdlet

And then .NET Reflector pops up with the right assembly opened up and expanded and everything. It's really pretty cool. Here's a screenshot:

Alt text http://halr9000.com/images/screenshots/reflector.png


I think if you were just starting PowerShell, this is what you'd be looking for:

$metadata = New-Object system.management.automation.commandmetadata (Get-Command Get-Process)
[System.management.automation.proxycommand]::Create($MetaData) | out-file C:\powershell\get-process.ps1

This will create a script which shows how Get-Process runs. Put in any cmdlet you want to replace Get-Process. If you want to google more about it, this is how you would create a proxy function.


For compiled Cmdlets, you can get the path to the .dll with:

(Get-Command Get-ChildItem).DLL

(Replace Get-ChildItem with the cmdlet you are interested in)

Once you know the path to the .dll, you can open it with a .NET disassembler like dotPeek:

& dotPeek64.exe (Get-Command Get-ChildItem).DLL

You might also like to take a look at Windows Installer PowerShell Snap-In on CodePlex. It's a smaller project than the community extensions, so it is easier to get your head around what's going on.

Check out Professional Windows PowerShell Programming: Snapins, Cmdlets, Hosts and Providers (Wrox Professional Guides), ISBN: 0470173939 - it's one of the most useful books I've found for writing cmdlets and providers.


You should be able to use .NET Reflector to "see" the source code. You need to know the assembly though, but it should also accessible using the GetType method or similar.

This PowerShellLanguage .NET Reflector Add-In can perhaps be useful.


PowerShell cmdlets' assemblies are located in GAC. You can find "Get-ChildItem" cmdlet in:

Microsoft.PowerShell.Commands.Management assembly, Microsoft.PowerShell.Commands.GetChildItemCommand class.

I've used ILSpy .NET decompiler and filtered GAC assemblies by "powershell" string. As I understand, Microsoft.PowerShell.Commands.* assemblies contain built-in cmdlets.