Skip to content

Instantly share code, notes, and snippets.

@eizedev
Last active July 11, 2024 04:52
Show Gist options
  • Save eizedev/e2bdfe678845b7143540bf91ade0c1c9 to your computer and use it in GitHub Desktop.
Save eizedev/e2bdfe678845b7143540bf91ade0c1c9 to your computer and use it in GitHub Desktop.
Get SourceCode of any powershell cmdlet/function by using the command metadata
function Get-CommandSourceCode
{
<#
.SYNOPSIS
Get SourceCode of an powershell cmdlet
.DESCRIPTION
Get SourceCode of an powershell cmdlet
.PARAMETER Command
Must be a valid powershell cmdlet/function
.EXAMPLE
Get-CommandSourceCode -Command "Get-Process"
.EXAMPLE
"Get-Process" | Get-CommandSourceCode
.NOTES
Last Modified: Aug 11, 2021
Version: 1.1
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromPipeline = $True
)]
[ValidateScript(
{
if ( -Not (Get-Command $_) )
{
throw "ERROR: Command '$_' not found"
}
return $true
}
)]
[string]
$Command
)
$Metadata = [System.Management.Automation.CommandMetadata]::new((Get-Command $Command))
[System.Management.Automation.ProxyCommand]::Create($MetaData)
}
@eizedev
Copy link
Author

eizedev commented Aug 11, 2021

Example of metadata for Get-Process:

$metadata

Name                    : Get-Process
CommandType             : Microsoft.PowerShell.Commands.GetProcessCommand
DefaultParameterSetName : Name
SupportsShouldProcess   : False
SupportsPaging          : False
PositionalBinding       : True
SupportsTransactions    : False
HelpUri                 : https://go.microsoft.com/fwlink/?LinkID=2096814
RemotingCapability      : SupportedByCommand
ConfirmImpact           : None
Parameters              : {[Name, System.Management.Automation.ParameterMetadata], [Id, System.Management.Automation.ParameterMetadata], [InputObject, System.Management.Automation.ParameterMetadata], [IncludeUserName,
                          System.Management.Automation.ParameterMetadata]…}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment