2

With my first C# based Powershell cmdlet I ran into an issue of how to run "existing" cmdlets from within my custom cmdlet. Along the way I found the following:

InvokeCommand.GetCmdlet("Compress-7Zip") 

and

InvokeCommand.GetCommand("Compress-7Zip")  
  1. Edit The first returns a CmdletInfo object the second a CommandInfo object
  2. GetCmdlet has a description "Returns CmdletInfo object that corresponds to the name argument"
  3. GetCommand has no description, but searching on the web I found the following on Mirosoft's website "Activity to invoke the Microsoft.PowerShell.Core\Get-Command command in a Workflow"

Why do these two methods exist and which do we use for what?

Jacques
  • 6,936
  • 8
  • 43
  • 102

1 Answers1

1

Get-Cmdlet - Returns the CmdletInfo object that corresponds to the 'Name' argument.

Get-Command - gets all commands that are 'Installed' on the computer, including cmdlets, aliases, functions, filters, scripts, and applications.

Get-Cmdlet

https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.commandinvocationintrinsics.getcmdlet?view=powershellsdk-7.0.0

Get-Command https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-command?view=powershell-7.2

NeoTheNerd
  • 566
  • 3
  • 11
  • Interesting, the method signature for GetCommand in C# provides a parameter of commandName (singular) suggesting a single command and the return type is a CommandInfo object, not a list. Also, your second link refers to Get-Command automatically importing the module if not loaded. My thinking is that Get-Command in this case behaves, or is used, differently from the GetCommand in the SDK? – Jacques Dec 03 '21 at 14:29