Command to Get Parameter Alias

This topic contains 3 replies, has 3 voices, and was last updated by  postanote 3 weeks, 1 day ago.

  • Author
    Posts
  • #97629

    Lockdown
    Participant

    Does anyone know of a command or commands to get a parameter's alias ?

    This does NOT work:

    (get-command get-eventlog | select -ExpandProperty parameters)
    .computername.aliases

  • #97637

    Lev Leiderman
    Participant

    Try this:

    (Get-Command Get-EventLog).parameters.values |
    where aliases | select name, aliases
  • #97709

    postanote
    Participant

    To add to what Lev gave you. for looking up stuff / getting deeper details. This is a little block item I keep I'm my personal module library (dot sourced to my user profile and in my snippets folder) to quickly look up / discover stuff, which includes what Lev already gave you.

    # Get parameters, examples, full and Online help for a cmdlet or function
    
    # Get a list of all functions
    Get-Command -CommandType Function | 
    Out-GridView -PassThru -Title 'Available functions'
    
    # Get a list of all commandlets
    Get-Command -CommandType Cmdlet | 
    Out-GridView -PassThru -Title 'Available cmdlets'
    
    # Get a list of all functions for the specified name
    Get-Command -Name '*ADGroup*' -CommandType Function | 
    Out-GridView -PassThru -Title 'Available named functions'
    
    # Get a list of all commandlets for the specified name
    Get-Command -Name '*ADGroup**'  -CommandType Cmdlet | 
    Out-GridView -PassThru -Title 'Available named cmdlet'
    
    # get function / cmdlet details
    (Get-Command -Name Get-ADUser).Parameters
    Get-help -Name Get-ADUser -Examples
    Get-help -Name Get-ADUser -Full
    Get-help -Name Get-ADUser -Online
    
    Get-Help about_*
    Get-Help about_Functions
    
    # Find all cmdlets / functions with a target parameter
    Get-Command -CommandType Function | 
    Where-Object { $_.parameters.keys -match 'credential'} | 
    Out-GridView -PassThru -Title 'Available functions which has a specific parameter'
    
    Get-Command -CommandType Cmdlet | 
    Where-Object { $_.parameters.keys -match 'credential'} | 
    Out-GridView -PassThru -Title 'Available cmdlets which has a specific parameter'
    
    # Get namesd aliases 
    Get-Alias | 
    Out-GridView -PassThru -Title 'Available cmdlets which has a specific parameter'
    
    # Get cmdlet / function parameter aliases
    (Get-Command Get-ADUser).Parameters.Values | 
    where aliases | 
    select Name, Aliases
    
    
    # All Help topics locations
    explorer "$pshome\$($Host.CurrentCulture.Name)"
    
    # Get any .NET types and their static methods from PowerShell. 
    # Enumerate all that are currently loaded into your AppDomain.
    #  
    [AppDomain]::CurrentDomain.GetAssemblies() | 
    foreach { $_.GetTypes() } | 
    foreach { $_.GetMethods() } | 
    where { $_.IsStatic } | 
    select DeclaringType, Name | 
    Out-GridView -PassThru -Title '.NET types and their static methods'
    
    # Instantiate the types using new-object and call instance methods. 
    # You can use get-member on an instance to get the methods on a type.
    

    Also, in my module, if I am really curious as to what a cmdlet or function is doing. I have a pair of functions to look at the source code. The functions work on virtually every cmdlet and function you may come across, well almost everyone as I've encountered a few where the code would not popup for them. Of course since PoSH is now Open Sourced (well v6 (PSCore is) – 'github.com/powershell/powershell'), you can just download all the source from the MS Github repo as well and ore contribute back of course.

    Looking at the source is also a interesting tutorial on the authors mindset and style.

    See also:

    PowerShell scripting best practices
    'blogs.technet.microsoft.com/pstips/2014/06/17/powershell-scripting-best-practices'

    The Unofficial PowerShell Best Practices and Style Guide
    'blogs.technet.microsoft.com/pstips/2014/06/17/powershell-scripting-best-practices

    Using PSScriptAnalyzer to check your PowerShell code for best practices
    'mikefrobbins.com/2015/11/19/using-psscriptanalyzer-to-check-your-powershell-code-for-best-practices'

    Point of note:
    Running a some of the lookup commands can take a good while for results to show, depending on how many cmdlets / functions are on your system. You may see some errors depending on whether X or Y actually exists (or are readable at all) or you may not be running as Admin, thus some locations would get access denied.

    Just a note about aliases at any level:
    There a good thing for Q&D (quick and dirty – avoid the carpel tunnel) interactive command line stuff, or if it's just you who are going to use it. In production scripts/automation, they are generally a bad thing. Due to folks just not familiar (or don't care to learn) with them, not willing to use them, and then there is the maintenance of that code, especially for those that follow you. Aliases just make things have unnecessary efforts to decipher them.

    So, sure use them, many do, but in production code, best practice to always use full cmdlet/function/parameter, to avoid any further confusion / unnecessary hair pulling (I tell my crew, I have none left to pull – 8^}).

    BTW, in the PoSH console host and the ISE, it will not yell at you (meaning, show error/issue indicators – well it will if you validate with the PSScriptAnalyzer) not to use them, but Visual Studio Code will dynamically, with messages like (when you hover over the error in the editor) ...

    [PSScriptAnalyzer]
    'gci' is an alias of 'Get-ChildItem'. Alias can introduce possible problems and make scripts hard to maintain. Please consider changing alias to its full content. (PSAvoidUsingCmdletAliases)
    function gci

    ... and even offer to expand it for you at a click.

    You can use a function on your production code to dynamically expand aliases, so that it is not a manual thing for you to deal with later.

You must be logged in to reply to this topic.