Last active
July 11, 2024 04:52
-
-
Save eizedev/e2bdfe678845b7143540bf91ade0c1c9 to your computer and use it in GitHub Desktop.
Get SourceCode of any powershell cmdlet/function by using the command metadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of metadata for
Get-Process
: