The power of Trace-Command is amazing to help troubleshoot a command or to gain better insight into what goes on behind the scenes when a command or commands are running.
What you may or may not know is that any of the cmdlets/functions that have the Get verb can actually be run without the “Get-“ prefix. Well, all with the exception of Get-Process because if you just use Process, it will throw an error because if is a Keyword used with Begin,Process and End. Still don’t believe me, check this out!
Acl Date ChildItem
Ok, so what exactly is happening here? From here, we really can’t see what is going on, but obviously something must be happening as these are not aliases and I cannot find this using Get-Command (or Command; see what I did there ).
The answer lies in Trace-Command, something that I have talked about in the past when it comes to looking at what is happening behind the scenes with different commands to troubleshoot various issues. While this isn’t an ‘issue’, it certainly falls along the lines of ‘what is happening here’ and Trace-Command does a fine job of showing me just that.
First I need to figure out exactly what traces I need to use with Trace-Command. This can be accomplished by looking at Get-TraceSource and picking whichever one seems to fit my needs.
Get-TraceSource | Format-Wide -Property Name -Column 3
In this case, I need to look at using CommandDiscovery as the means to find out why I do not really need the Get- in my Get-* commands.
Ok, now that I know this, I can run Trace-Command and look at what happens when I just use Acl vs. Get-Acl.
Trace-Command -Name CommandDiscovery -Expression {Acl} -PSHost
Expect a bunch of Debug output to flood your screen. The notable spots will be featured below.
The command cannot be found as a cmdlet, so it is now beginning to look up the command in other places, such as in the $Env:Path and then begins looking at the System32 folder using Acl with all extensions using all paths under Env:Path.
Eventually, it will run out of possible places to look for Acl and trying to plug all types of extensions to it and will then prepend Acl with Get-. Once it does that, the process begins yet again. This time it very quickly finds Get-Acl and proceeds to run the command.
What happens if we run the trace using Get-Acl?
We get exactly the same thing that we saw at the last part of the trace with Acl. Note that running any Get- command without the Get- will work with the exception of Process (as I mentioned earlier) and this includes any functions that you have created.
So there you have it. Another mystery solved by Trace-Command! While the output can be a little much, this command can really help out when you are trying to figure out what a command is not behaving like you think it should.
Pingback: Using Cmdlets without Get- Prefix… What?! : PSB E05 – PowerShell Bytes
Pingback: The Mystery of the PowerShell “alias” » Coding Geekette