Short description
In Windows PowerShell, an alias is an alternative name for cmdlets or other commands.
Detailed description
An alias is a substitute name or a nickname for cmdlets or commands (for example: functions, scripts, files, executables). You can use alias where commands are used.
The name of a cmdlet consists of a verb and a noun, and its function is clear to the user. But it is also troublesome for a person who often uses powershell commands to type so many commands every day. Can you shorten the command a little? So the "alias" came into being. Powershell also implements alias for many commonly used commands. For example, Get-ChildItem, lists the current subfile or directory. It has two alias: ls and dir, which are derived from the shell of unix and the cmd of windows.
Therefore, alias has two functions:
Inheritance: Inherit unix-shell and windows-cmd.
Convenient: Convenient to users.
Handle alias:
Query the real cmdlet command referred to by the alias.
PS C:\PS> Get-Alias -name ls CommandType Name Definition ----------- ---- ---------- Alias ls Get-ChildItem PS C:\PS> Get-Alias -name dir CommandType Name Definition ----------- ---- ---------- Alias dir Get-ChildItem PS C:\PS> Get-Alias -name fl CommandType Name Definition ----------- ---- ---------- Alias fl Format-List PS C:\PS> Get-Alias -name ft CommandType Name Definition ----------- ---- ---------- Alias ft Format-Table
View available aliases
View available alias, either via "ls alias:" or "Get-Alias"
How to view the alias of all cmdlet commands that are headed by Remove?
PS C:\PS> dir alias: | where {$_.("Remove")} CommandType Name Definition ----------- ---- ---------- Alias del Remove-Item Alias erase Remove-Item Alias rbp Remove-PSBreakpoint Alias rd Remove-Item Alias rdr Remove-PSDrive Alias ri Remove-Item Alias rjb Remove-Job Alias rm Remove-Item Alias rmdir Remove-Item Alias rmo Remove-Module Alias rp Remove-ItemProperty Alias rsn Remove-PSSession Alias rsnp Remove-PSSnapin Alias rv Remove-Variable Alias rwmi Remove-WMIObject
Description: dir alias: Gets an alias array, traversing the array elements through where, $_ represents the current element, and the Definition of alias is of String type, because powershell supports .net, and the string class in .net has a method Startswith. Filtering collections by where is used very widely in powershell.
Some cmdlet commands may have 2-3 aliases. We can use the following command to view all aliases and the number of aliases pointing to cmdlets.
PS C:\PS> ls alias: | Group-Object definition | sort -Descending Count Count Name Group ----- ---- ----- 6 Remove-Item {del, erase, rd, ri...} 3 Set-Location {cd, chdir, sl} 3 Get-History {ghy, h, history} 3 Get-ChildItem {dir, gci, ls} 3 Get-Content {cat, gc, type} 3 Move-Item {mi, move, mv} 3 Copy-Item {copy, cp, cpi} 2 Start-Process {saps, start} 2 Set-Variable {set, sv} 2 Write-Output {echo, write} 2 Get-Process {gps, ps} 2 Invoke-History {ihy, r} 2 New-PSDrive {mount, ndr} 2 Stop-Process {kill, spps} 2 Rename-Item {ren, rni} 2 Get-Location {gl, pwd} 2 Compare-Object {compare, diff} 2 Where-Object {?, where} 2 ForEach-Object {%, foreach} 2 Clear-Host {clear, cls} 1 Out-Host {oh} 1 New-PSSession {nsn} 1 New-Variable {nv} 1 Out-GridView {ogv} 1 Pop-Location {popd} 1 Tee-Object {tee} 1 Remove-PSBreakpoint {rbp} 1 Receive-Job {rcjb} 1 Push-Location {pushd} 1 mkdir {md} 1 Measure-Object {measure} 1 help {man} 1 Remove-PSSnapin {rsnp} 1 Out-Printer {lp} 1 New-Item {ni} 1 New-Module {nmo} 1 New-Alias {nal} 1 Move-ItemProperty {mp} 1 Wait-Job {wjb} 1 Remove-PSDrive {rdr} 1 Start-Service {sasv} 1 Set-PSBreakpoint {sbp} 1 Set-ItemProperty {sp} 1 Start-Job {sajb} 1 Set-Alias {sal} 1 Start-Sleep {sleep} 1 Set-Item {si} 1 Select-Object {select} 1 Set-Content {sc} 1 Sort-Object {sort} 1 Remove-WMIObject {rwmi} 1 Remove-Module {rmo} 1 Rename-ItemProperty {rnp} 1 Stop-Service {spsv} 1 Set-WMIInstance {swmi} 1 Remove-Job {rjb} 1 Remove-Variable {rv} 1 Resolve-Path {rvpa} 1 Stop-Job {spjb} 1 Remove-ItemProperty {rp} 1 Remove-PSSession {rsn} 1 Exit-PSSession {exsn} 1 Format-Custom {fc} 1 Enter-PSSession {etsn} 1 Export-Csv {epcsv} 1 Export-PSSession {epsn} 1 Format-List {fl} 1 Get-PSBreakpoint {gbp} 1 Get-Command {gcm} 1 Get-Alias {gal} 1 Format-Table {ft} 1 Format-Wide {fw} 1 Export-Alias {epal} 1 Clear-History {clhy} 1 Clear-Item {cli} 1 Clear-Content {clc} 1 Add-Content {ac} 1 Add-PSSnapIn {asnp} 1 Clear-ItemProperty {clp} 1 Disable-PSBreakpoint {dbp} 1 Enable-PSBreakpoint {ebp} 1 Convert-Path {cvpa} 1 Clear-Variable {clv} 1 Copy-ItemProperty {cpp} 1 Invoke-Expression {iex} 1 Invoke-Item {ii} 1 Invoke-Command {icm} 1 Get-Variable {gv} 1 Get-WmiObject {gwmi} 1 Import-Alias {ipal} 1 powershell_ise.exe {ise} 1 Invoke-WMIMethod {iwmi} 1 Import-PSSession {ipsn} 1 Import-Csv {ipcsv} 1 Import-Module {ipmo} 1 Get-Unique {gu} 1 Get-Job {gjb} 1 Get-Member {gm} 1 Get-Item {gi} 1 Get-PSCallStack {gcs} 1 Get-PSDrive {gdr} 1 Get-Module {gmo} 1 Get-PSSnapIn {gsnp} 1 Get-Service {gsv} 1 Get-PSSession {gsn} 1 Get-ItemProperty {gp} 1 Group-Object {group}
Create your own alias
Create an alias for Notepad and view the alias;
PS C:\PS> Set-Alias -Name Edit -Value notepad PS C:\PS> Edit PS C:\PS> $alias:Edit notepad
Delete your own alias
Aliases do not need to be deleted, custom aliases will be automatically cleared when powershell exits. But rest assured that powershell built-in aliases (such as ls, dir, fl, etc.) will not be cleared. If you have to delete the alias manually. Please use
PS C:\PS> del alias:Edit saves its own alias
You can use Export-Alias to export the alias to the file, and then import it through Import-Alias if needed. However, there may be an exception when importing, prompting that the alias already exists and cannot be imported:
PS C:\PS> Import-Alias alias.ps1 Import-Alias : Alias not allowed because an alias with the name 'ac' already exists. At line:1 char:13 + Import-Alias <<<< alias.ps1 + CategoryInfo : ResourceExists: (ac:String) [Import-Alias], SessionStateException + FullyQualifiedErrorId : AliasAlreadyExists,
At this time, you can use Force to force import.
PS C:\PS> Export-Alias alias.ps1 PS C:\PS> Import-Alias -Force alias.ps1
For example, if you set the alias "gas" for Get-AuthenticodeSignature, you can type directly:
gas c:\scripts\sqlscript.ps1
Without having to enter:
get-authenticodesignature c:\scripts\sqlscript.ps1
If you set the alias "word" for Microsoft Word, you can type directly:
word
Without having to enter:
"c:\program files\microsoft office\office11\"
Predefined alias
Windows PowerShell has predefined some alias, for example: "cd" and "chdir" are both alias for Set-Location, and "ls" and "dir" are alias for Get-Childitem.
Find all aliases in the system (including predefined aliases), enter the following command:
get-alias
CMDLETS related to alias
Windows PowerShell contains several cmdlets for manipulating aliases.
Get-Alias: Get the alias in the current session.
· New-Alias: Create a new alias.
Set-Alias: Create or modify an alias.
Export-Alias: Export one or more aliases into a file.
Import-Alias: Import a different file into Windows PowerShell.
Need details of cmdlets, enter:
get-help <cmdlet-name> -detailed
For example:
get-help export-alias -detailed
Create an alias
Create a new alias, you can use the New-Alias cmdlet. For example, to create a "gh" alias for Get-Help, enter,
new-alias -name gh -value get-help
You can use this alias in the command as if the full cmdlet name and various parameters you are using.
For example, to obtain the details of Get-WmiObject cmdlet, you just enter:
get-help get-wmiobject -detailed
or
gh get-wmiobject -detailed
Save the alias
The alias you create only work in the current session. To use alias in different sessions, you must write the definition of the alias to your Windows PowerShell configuration file, or use Export-Alias to store the alias in the file.
Find an alias
To display all aliases on the current console, including Windows PowerShell predefined aliases, the aliases defined in your Windows PowerShell configuration file, the aliases you created in the current session, just enter:
get-alias
If a specific alias is needed, specify the Name parameter for Get-Alias. For example, to get an alias starting with "p", enter
get-alias -name p*
To find all aliases for a specific cmdlet, you can enter:
get-alias | where-object {$_.Definition -eq "<cmdlet-name>"}
For example:
get-alias | where-object {$_.Definition -eq "Remove-Item"}
Create an alias for commands with parameters
You can assign alias to cmdlets, scripts, functions, or executables. But you can't set alias for commands with parameters. For example, you can set an alias for "Get-Eventlog", but you can't set an alias for "Get-Eventlog -logname security".
You can only solve this problem by creating a function containing the command. For example, the following command creates a function called "seclog", which can represent the "get-eventlog -logname security" command.
function seclog {get-eventlog -logname security}
Now you can enter the name "seclog" to simplify the previous command, and you can also create alias for the function "seclog".
Information about functions, enter:
get-help about_function
Alias Object
The Windows PowerShell alias is actually an instance object of a class. For object type information, see the topic "AliasInfo Class" in MSDN.
To view properties and methods on an alias object, first get the alias object and pipe it to the Get-Member cmdlet. For example,
get-alias | get-member
To view the property value of a specific alias, such as the alias "dir", get the alias and pipe it to the Format-List cmdlet. For example, the following code first obtains the alias "dir" object, pipes it to the Format-List cmdlet, and displays all properties of the alias "dir" by assigning the parameter Property of Format-List to all (*) to the Format-List parameter Property to display.
get-alias -name dir | format-list -property *
WINDOWS POWERSHELL alias PROVIDER
The Windows PowerShell alias provider (Translator's note: A provider is similar to the file system directory structure used by users. Microsoft developers use the design idea of MVC to abstract the management of resources such as variables, registries, aliases into file system management. This way users can access various resources using a unified syntax. PowerShell developers can also extend other providers for PowerShell, making it like browsing file system drives in Windows PowerShell.
The alias provider provides the "Alias:" drive (Translator's note: virtual drive, only valid in PowerShell). To enter the Alias: drive, enter:
set-location alias:
To view the contents of the drive, enter:
get-childitem
When you want to view the alias drive in Windows PowerShell, if you want to view the alias drive, you must negotiate the drive name before the directory, followed by a colon (:). For example,
get-childitem -path alias:
To obtain information for a specific alias, enter the drive name and alias name, or the pattern of the name (pattern. Author's note: Wildcards are generally used.). For example, to obtain a list of all aliases starting with "p", enter:
get-childitem -path alias:p*
Need more information about the Windows PowerShell alias provider, enter:
get-help alias-psprovider
You can also refer to
To list cmdlets about alias, enter:
get-help *-Alias
Information about functions, enter:
get-help about_function