2

Could I override this command? I want to do my own job when I execute the "Get-ChildItem" command in Powershell.

  • 1
    What are you trying to accomplish? We can help you better if we know what you are trying to do. – Davidw Nov 5 '14 at 3:42
  • I'm try to simulate some powershell command provided by other company(XenDesktop), and all of the other commands have already been simulated, but at the last of the task, I found one of the command, "Get-ChildItem", is the command provided by powershell itself, I want to know could I override it. – jason Nov 5 '14 at 3:53
  • For example, XenDesktop provides a "Get-ConfigSite" powershell command, and I have no idea about the logical of the command script, but I know the result of it, so I will add the same name "Get-ConfigSite.ps1" file in my compute, and in the file, I will use powershell script to create the simulated result. All of the task work well, until I met the "get-childitem" command, which is provided by powershell, that means it already exsits in my computer, so I want to override it, or how can I let powershell use my script when I execute "Get-ChildItem" command. – jason Nov 5 '14 at 3:58
6

Yes, you can override Get-ChildItem or any other cmdlet in Powershell.

Name Your Function The Same

If you make a function with the same name in the same scope, yours will be used.

Example:

Function Get-ChildItem {
[CmdletBinding()]
param(
    # Simulate the parameters here
)
    # ... do stuff
}

Using Aliases

Create your own function, and then create an alias to that function, with the same name as the cmdlet you want to override.

Example:

Function My-GetChildItem {
[CmdletBinding()]
param(
    # Simulate the parameters here
)
    # ... do stuff
}

New-Alias -Name 'Get-ChildItem' -Value 'My-GetChildItem' -Scope Global

This way is nice because it's easier to test your function without stomping on the built-in function, and you can control when the cmdlet is overridden or not within your code.

To remove the alias:

Remove-Item 'Alias:\Get-ChildItem' -Force

Know the Command Precedence

about_Command_Precedence lists the order in which commands of different types are interpreted:

If you do not specify a path, Windows PowerShell uses the following precedence order when it runs commands:

  1. Alias
  2. Function
  3. Cmdlet
  4. Native Windows commands
  • Thanks @briantist, I have solved it using the first solution. I created a profile.ps1 at the path of $pshome and I add my functions in it, so that every session will load this profile, also load my functions. It works fine in the local situation, but when I used remote powershell to connect to my computer, these functions cannot be invoked, and a CommnadNotFoundException was thrown. Do you know the reason of it? – jason Nov 14 '14 at 3:22
  • By the way, does you mean add a function to the profile.ps1 when you said "Name the function the same", since I noticed the syntax is not the same with my function definition in profile.ps1. – jason Nov 14 '14 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.