文章目录
powershell 的cmdlet名字大多比较长,如果使用别名的话可以提高效率
由于powershell 不区分大小写,所以默认在搜索相关命令的时候不区分大小写
相关文档
help get-alias
help get-alias -examples
help gal -examples
get-alias 支持双向查询别名
get -Name
根据别名查询原命令
get -Definition
根据原命令查询别名配置情况
默认get-alias
默认情况下,该命令根据别名来查找命令(函数)的原名(Definition)
get-alias(gal)返回的对象分析
PS D:\repos\learnPwsh> gal|gm
TypeName: System.Management.Automation.AliasInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.…
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ResolveParameter Method System.Management.A…
ToString Method string ToString()
CommandType Property System.Management.A…
Definition Property string Definition {
…
Description Property string Description …
Module Property psmoduleinfo Module…
ModuleName Property string ModuleName {
…
Name Property string Name {
get;}
Options Property System.Management.A…
OutputType Property System.Collections.…
Parameters Property System.Collections.…
ParameterSets Property System.Collections.…
ReferencedCommand Property System.Management.A…
RemotingCapability Property System.Management.A…
ResolvedCommand Property System.Management.A…
Source Property string Source {
get;}
Version Property version Version {
ge…
Visibility Property System.Management.A…
DisplayName ScriptProperty System.Object Displ…
HelpUri ScriptProperty System.Object HelpU…
ResolvedCommandName ScriptProperty System.Object Resol…
示例
文档示例
------ Example 1: Get all aliases in the current session ------
Get-Alias
CommandType Name
----------- ----
Alias % -> ForEach-Object
Alias ? -> Where-Object
Alias ac -> Add-Content
Alias asnp -> Add-PSSnapin
Alias cat -> Get-Content
Alias cd -> Set-Location
Alias chdir -> Set-Location
Alias clc -> Clear-Content
Alias clear -> Clear-Host
Alias clhy -> Clear-History
...
This command gets all aliases in the current session.
The output shows the `<alias> -> <definition>` format that was introduced in Windows
PowerShell 3.0. This format is used only for aliases that do not include hyphens,
because aliases with hyphens are typically preferred names for cmdlets and functions,
rather than nicknames.
---------------- Example 2: Get aliases by name ----------------
Get-Alias -Name gp*, sp* -Exclude *ps
This command gets all aliases that begin with gp or sp, except for aliases that end
with ps.
------------- Example 3: Get aliases for a cmdlet -------------
Get-Alias -Definition Get-ChildItem
This command gets the aliases for the `Get-ChildItem` cmdlet.
By default, the `Get-Alias` cmdlet gets the item name when you know the alias. The
Definition parameter gets the alias when you know the item name.
-------------- Example 4: Get aliases by property --------------
Get-Alias | Where-Object {
$_.Options -Match "ReadOnly"}
This command gets all aliases in which the value of the Options property is ReadOnly
. This command provides a quick way to find the aliases that are built into
PowerShell, because they have the ReadOnly option. Options is just one property of
the AliasInfo objects that `Get-Alias` gets. To find all properties and methods of
AliasInfo objects, type `Get-Alias | get-member`.
Example 5: Get aliases by name and filter by beginning letter
Get-Alias -Definition "*-PSSession" -Exclude e* -Scope Global
This example gets aliases for commands that have names that end in "-PSSession",
except for those that begin with "e".
The command uses the Scope parameter to apply the command in the global scope. This
is useful in scripts when you want to get the aliases in the session.
获取alias相关的命令的别名
Get-Alias -Definition *alias*
CommandType Name Version Source
----------- ---- ------- ------
Alias epal -> Export-Alias
Alias gal -> Get-Alias
Alias ipal -> Import-Alias
Alias nal -> New-Alias
Alias sal -> Set-Alias
Alias setAlias -> Set-Alias
看到get-Alias
配置了别名gal
所以之后我们直到可以用gal来代替get-alias
好,现在我们利用已经获得的知识去查询经常使用的命令的别名配置情况
例如select-object
,where-object
,sort-object
根据原命令名查找可能存在的别名
-Definition
参数
PS C:\Users\cxxu\Desktop\tes> gal -Definition where*
CommandType Name Version Source
----------- ---- ------- ------
Alias ? -> Where-Object
Alias where -> Where-Object
PS C:\Users\cxxu\Desktop\tes> gal -Definition *select*
CommandType Name Version Source
----------- ---- ------- ------
Alias select -> Select-Object
Alias sls -> Select-String
PS C:\Users\cxxu\Desktop\tes> gal -Definition *sort*
CommandType Name Version Source
----------- ---- ------- ------
Alias sort -> Sort-Object
PS C:\Users\cxxu\Desktop\tes> gal -Definition *member*
CommandType Name Version Source
----------- ---- ------- ------
Alias gm -> Get-Member
现用查询到的别名,来做一个信息(列)筛选
PS C:\Users\cxxu\Desktop\tes> gal -Definition *alias* | select DisplayName|Format-Table
DisplayName
-----------
epal -> Export-Alias
gal -> Get-Alias
ipal -> Import-Alias
nal -> New-Alias
sal -> Set-Alias
setAlias -> Set-Alias
这里可以提一下获取对象(真实(完整)属性)的若干方法
获取对象(真实(完整)属性)的若干方法
PS C:\Users\cxxu\Desktop\tes> gal -Definition *alias*|select -First 1|select *
HelpUri : https://go.microsoft.com/fwlink/?LinkID=2096597
ResolvedCommandName : Export-Alias
DisplayName : epal -> Export-Alias
ReferencedCommand : Export-Alias
ResolvedCommand : Export-Alias
Definition : Export-Alias
Options : ReadOnly
Description :
OutputType : {
System.Management.Automation.AliasInfo}
Name : epal
CommandType : Alias
Source :
Version :
Visibility : Public
ModuleName :
Module :
RemotingCapability : PowerShell
Parameters : {
[Path, System.Management.Automation.ParameterMetadata],
[LiteralPath, System.Management.Automation.ParameterMetadata],
[Name, System.Management.Automation.ParameterMetadata], [PassThru,
System.Management.Automation.ParameterMetadata]…}
ParameterSets :
这中方法的一个好处,可以看到属性以及相应的取值的实例,帮助您更好的把握属性的含义.
当然,还不清楚某些相像的属性的区别,可以利用get-member
(gm)这样做
扫描二维码关注公众号,回复:
13412539 查看本文章
PS C:\Users\cxxu\Desktop\tes> gal -Definition *member*|gm
TypeName: System.Management.Automation.AliasInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ResolveParameter Method System.Management.Automation.ParameterMetadata Resolve…
ToString Method string ToString()
CommandType Property System.Management.Automation.CommandTypes CommandType …
Definition Property string Definition {
get;}
Description Property string Description {
get;set;}
Module Property psmoduleinfo Module {
get;}
ModuleName Property string ModuleName {
get;}
....(我节约篇幅,做了删节)
DisplayName ScriptProperty System.Object DisplayName {
get=if ($this.Name.IndexOf(…
HelpUri ScriptProperty System.Object HelpUri {
get=$oldProgressPreference = $P…
ResolvedCommandName ScriptProperty System.Object ResolvedCommandName {
get=$this.ResolvedC…
这包含了属性/方法的解释