PowerShell - How do I call a cmdlet in a function when overriding that cmdlet's name with the same name as the function?
Asked Answered
C

3

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.

Clubman answered 5/4, 2011 at 18:29 Comment(0)
D
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
Disruptive answered 5/4, 2011 at 19:26 Comment(4)
Is it better to go this route or the route of the answer I received off of twitter (somewhere in this set of answers)?Clubman
Both will work unless you have multiple cmdlets with the same name in which case 'Get-Command <blah> -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.Disruptive
Good enough for me. Thank you sir.Clubman
Good example: $oc = Get-Command 'Write-Host' -Module 'Microsoft.PowerShell.Utility'Clubman
C
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
}
Clubman answered 5/4, 2011 at 19:38 Comment(1)
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.Turmoil
W
1

Can you use a proxy function?

Wingspread answered 5/4, 2011 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.