I'm learning some PowerShell. Is it possible to see the source code for a built-in cmdlet like Get-ChildItem?
I'm learning some PowerShell. Is it possible to see the source code for a built-in cmdlet like Get-ChildItem?
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.
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.
For some cmdlets that are installed with Install-Module and contain source code and not binaries, (e.g. PSnmap):
Install-Module -Name PSnmap
...you can view their code by looking at the Definition property from:
Get-Command Invoke-PSnmap | Format-List
If not, most likely, you will need to decompile some binary (e.g. the file specified in the DLL property).