Kamis, 21 Juli 2016

PowerShell v2 You Can Omit Get Heres Why

So, I saw this post fly across the Twittersphere earlier today:
Ifiok Moses @mifiok
RT @bradwilson: Just discovered today that in PowerShell, you can leave off "get-". Seriously, try it: type "childitem" or "location". true
Not having heard of this, I tried it out and sure enough, it works. What I wanted to know was how. After pinging Justin about it he reminded me of the great and glorious Trace-Command cmdlet to check it out if I was really curious. Knowing we were looking at a pretty specific command to trigger this in Tracing I tried a few options and hit paydirt with the commandiscovery tracesource. Using this command:
Trace-Command -Name *commanddiscovery* -Expression {acl C: est} -PSHost
I got a good bit of general chattiness indicating it could not find any command in the usual places, i.e., the $env:path folders one by one. This is essentially the same pattern I outlined in this post:
Powershell (v3) - Tab Completion Takes Forever sometimes. Heres why (Be warned: Its long)
Once it looked in all its expected locations and file types it then tried a new trick I hadnt seen yet:
"trying again with get- prepended"
Heres the branch output after that switch:
DEBUG: CommandDiscovery Information: 0 : The command [acl] was not found, trying again with get- prepended
DEBUG: CommandDiscovery Information: 0 : Looking up command: get-acl
DEBUG: CommandDiscovery Information: 0 : Cmdlet found: Get-Acl Microsoft.PowerShell.Commands.GetAclCommand,
Microsoft.PowerShell.Security, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
Right off the bat a cmdlet named Get-Acl is found in the Microsoft.PowerShell.Commands namespace with type GetAclCommand. After the command was located DEBUG output ceased and regular output was returned. Again, Trace-Command was able to identify the underlying mechanics and explain this mystery rather quickly.

lamsim

About lamsim

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :