Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add Version and Source info to a powershell alias?

I a have number of aliases set in my powershell profile (start-up script).

So doing alias (or equivalent Get-Alias), will show you a list with headers, showing: CommandType, Name, Version and Source. However, for all but a few of them, the Version and Source fields are empty, and there are several other available fields as well, but only shown when you do:

# Get-Alias upmo |select *

HelpUri             : https://go.microsoft.com/fwlink/?LinkID=398576
ResolvedCommandName : Update-Module
DisplayName         : upmo -> Update-Module
ReferencedCommand   : Update-Module
ResolvedCommand     : Update-Module
Definition          : Update-Module
Options             : None
Description         :
OutputType          : {}
Name                : upmo
CommandType         : Alias
Source              : PowerShellGet
Version             : 1.6.7
Visibility          : Public
ModuleName          : PowerShellGet
Module              : PowerShellGet
RemotingCapability  : PowerShell
Parameters          : {[Name, System.Management.Automation.ParameterMetadata], 
                       [RequiredVersion, System.Management.Automation.ParameterMetadata],
                       [MaximumVersion, System.Management.Automation.ParameterMetadata], 
                       [Credential, System.Management.Automation.ParameterMetadata]...}
ParameterSets       :

The MS docs for Set-Alias does not say anything how to access or set these additional fields.


How do I add the Version and Source info to my own powershell alias?


Possibly related questions:

  • How to add version info to my powershell script?
  • How to alias a Powershell function?
like image 678
not2qubit Avatar asked Jan 28 '26 02:01

not2qubit


1 Answers

How do I add the Version and Source info to my own powershell alias?

You can't do that directly, because this information comes from an alias' enclosing module.

In other words: an alias itself doesn't contain this information, only the module that it is a part of, if any.

Conversely, this means that aliases defined outside of a module - such as interactively defined ones or ones defined via $PROFILE - do not contain this information.

Another thing to be mindful of: Get-Alias only recognizes aliases from modules that are already imported into the session - module auto-loading is not triggered in this case. This means that an alias from an auto-loading module is only known to Get-Alias if that module has already been loaded (imported) through other means, notably by invocation of any of the module's commands.


The following sample code creates a (temporary) module Foo, with a module manifest (*.psd1) in order to enable versioning, and adds two aliases, foo1 and foo2. The module is then imported, and Get-Command is used to get information about the aliases:

# Create module 'Foo':

# Create the module folder...
$tmpDir = [io.path]::GetTempPath() + '/' + $PID + '/Foo'
Push-Location (New-Item -Type Directory -Force $tmpDir)

# ... and a manifest with a version number and the aliases to export ...
New-ModuleManifest -Path ./Foo.psd1 -RootModule Foo.psm1 -ModuleVersion 1.0.1 -AliasesToExport foo1, foo2

# ... and the module implementation with the two aliases
@'
set-alias foo1 Get-Date
set-alias foo2 Get-Command
'@ > Foo.psm1

# Import the module. Add -Verbose to see import details.
Import-Module -Force ../Foo

# Get information about the aliases.
Get-Command -Type Alias foo1, foo2

# Clean up.
Pop-Location
Remove-Item -Recurse $tmpDir

The above outputs the following, which shows the aliases' module of origin as well as the latter's version number:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           foo1 -> Get-Date                                   1.0.1      Foo
Alias           foo2 -> Get-Command                                1.0.1      Foo
like image 103
mklement0 Avatar answered Jan 29 '26 18:01

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!