9

So I have a cmdlet named update-name that I have no access to change.

I have created a function named update-name (the same name as the cmdlet). How do I call the cmdlet from the function with the same name?

I've tried a few things and none of them seem to work.

function update-name {
param([string] something)
  #call cmdlet update-name here
}

There is a way to do it when it is just functions:

$unBackup = 'DefaultUpdateName'
if(!(Test-Path Function:\$unBackup)) {
    Rename-Item Function:\Update-Name $unBackup
}

function update-name {
  & $unName
}

Unfortunately that doesn't work if it is a CmdLet.

ferventcoder
  • 11,952
  • 3
  • 57
  • 90

3 Answers3

13

You the cmdlet's module name to disambiguate the names:

PS> Get-Command Start-Process | Format-Table ModuleName

ModuleName
----------
Microsoft.PowerShell.Management

PS> Microsoft.PowerShell.Management\Start-Process Notepad
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Is it better to go this route or the route of the answer I received off of twitter (somewhere in this set of answers)? – ferventcoder Apr 05 '11 at 19:40
  • 1
    Both will work unless you have multiple cmdlets with the same name in which case 'Get-Command -Type Cmdlet` will return multiple results. I prefer the approach I propose because it is easier to tell which `cmdlet` you are using because the associated module name is spelled out in the script. – Keith Hill Apr 05 '11 at 20:32
  • Good enough for me. Thank you sir. – ferventcoder Apr 06 '11 at 15:43
  • 1
    Good example: $oc = Get-Command 'Write-Host' -Module 'Microsoft.PowerShell.Utility' – ferventcoder Nov 19 '11 at 17:07
6

This will do the trick as well - thanks Keith Dahlby! http://twitter.com/dahlbyk/status/55341994817503232

$unName=Get-Command 'Update-Name' -CommandType Cmdlet;

function update-name {
  & $unName
}
ferventcoder
  • 11,952
  • 3
  • 57
  • 90
  • 3
    There may be multiple cmdlets called update-name. But if you module-qualify it (as per Keith's answer above,) the collision is much more unlikely. – x0n Apr 06 '11 at 03:07
1

Can you use a proxy function?

Mike Shepard
  • 17,466
  • 6
  • 51
  • 69