Tuesday, 1 September 2015

About Aliases

What is an Alias ?
Alias is an alternate name for an object,Such as Variable,File etc.

Here in PowerShell, we have somany Aliases which are built-in.And those aliases helps us to run cmdlets fastly, Acting as a time saver.

Note:Avoid using Aliases in Scripts(Post comes Later).

So lets take the cmdlet Get-Help again.

The aliases for Get-Help are help and man. you can see man is a native unix help command,Which shows the manual for a command.

What about other cmdlets, We have a cmdlet for Getting Alias.
How to find it, Do it in PowerShell way

PS>Get-Command -Verb *Get* -Noun *alias*

CommandType     Name                                               Version    Source                                                                                        
-----------     ----                                               -------    ------                                                                                        
Cmdlet          Get-Alias                                          3.1.0.0    Microsoft.PowerShell.Utility                                                                  

Here we use Get-Command to Find a cmdlet which is Probabbly having Get as Verb and Alias as Noun, We use wild card for reliability.

so you Got the cmdlet to find Alias is Get-Alias.

Lets Explore Get-Alias.
PS>Get-Help Get-Alias

NAME
    Get-Alias
    
SYNOPSIS
    Gets the aliases for the current session.
    
    
SYNTAX
    Get-Alias [[-Name] []] [-Exclude []] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable 
    []] [-Scope []] []
    
    Get-Alias [-Definition []] [-Exclude []] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] 
    [-InformationVariable []] [-Scope []] []
    

Here We got The syntax for Get-Alias.

Lets find some examples for this cmdlet.
PS>Get-Help Get-Alias -Examples

NAME
    Get-Alias
    
SYNOPSIS
    Gets the aliases for the current session.

-------------------------- EXAMPLE 1 --------------------------
 PS C:\>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.

PS>Get-Alias -Definition Get-ChildItem

CommandType     Name                                               Version    Source                                                                                        
-----------     ----                                               -------    ------                                                                                        
Alias           dir -> Get-ChildItem                                                                                                                                        
Alias           gci -> Get-ChildItem                                                                                                                                        
Alias           ls -> Get-ChildItem                                                                                                                                         


So here We got Aliases for Get-ChildItem cmdlet. dir,gci and ls are the Aliases.

How do we get Built-in aliases.

PS>Get-Alias

CommandType     Name                                               Version    Source                                                                                        
-----------     ----                                               -------    ------                                                                                        
Alias           % -> ForEach-Object                                                                                                                                         
Alias           ? -> Where-Object                                                                                                                                           
Alias           ac -> Add-Content                                                                                                                                           
Alias           asnp -> Add-PSSnapin                                                                                                                                        
Alias           cat -> Get-Content                                                                                                                                          
Alias           cd -> Set-Location                                                                                                                                          
Alias           CFS -> ConvertFrom-String                          3.1.0.0    Microsoft.PowerShell.Utility                                                                  
Alias           chdir -> Set-Location                                                                                                                                       
Alias           clc -> Clear-Content                                                                                                                                        
Alias           clear -> Clear-Host                                                                                                                                         
Alias           clhy -> Clear-History                                                                                                                                       
Alias           cli -> Clear-Item                                                                                                                                           
Alias           clp -> Clear-ItemProperty                                                                                                                                   
Alias           cls -> Clear-Host                                                                                                                                           
Alias           clv -> Clear-Variable                                                                                                                                       
Alias           cnsn -> Connect-PSSession                                                                                                                                   
Alias           compare -> Compare-Object                                                                                                                                   
Alias           copy -> Copy-Item                                                                                                                                           
Alias           cp -> Copy-Item                                                                                                                                             
Alias           cpi -> Copy-Item                                                                                                                                            
Alias           cpp -> Copy-ItemProperty                                                                                                                                    
Alias           curl -> Invoke-WebRequest                                                                                                                                   
Alias           cvpa -> Convert-Path                                                                                                                                        
Alias           dbp -> Disable-PSBreakpoint                                                                                                                                 
Alias           del -> Remove-Item                                                                                                                                          
Alias           diff -> Compare-Object                                                                                                                                      
Alias           dir -> Get-ChildItem                                                                                                                                        
Alias           dnsn -> Disconnect-PSSession                                                                                                                                
Alias           ebp -> Enable-PSBreakpoint                                                                                                                                  
Alias           echo -> Write-Output                                                                                                                                        
Alias           epal -> Export-Alias                                                                                                                                        
Alias           epcsv -> Export-Csv                                                                                                                                         
Alias           epsn -> Export-PSSession                                                                                                                                    
Alias           erase -> Remove-Item                                                                                                                                        
Alias           etsn -> Enter-PSSession                                                                                                                                     

We have a built-in drive for Aliases !.

Do  CD tio Alias Drive, And do ls (Get-ChildItem)

C:\>cd Alias:

Alias:\>ls

CommandType     Name                                               Version    Source                                                                                        
-----------     ----                                               -------    ------                                                                                        
Alias           % -> ForEach-Object                                                                                                                                         
Alias           ? -> Where-Object                                                                                                                                           
Alias           ac -> Add-Content                                                                                                                                           
Alias           asnp -> Add-PSSnapin                                                                                                                                        
Alias           cat -> Get-Content                                                                                                                                          
Alias           cd -> Set-Location                                                                                                                                          
Alias           CFS -> ConvertFrom-String                          3.1.0.0    Microsoft.PowerShell.Utility                                                                  
Alias           chdir -> Set-Location                                                                                                                                       
Alias           clc -> Clear-Content                                                                                                                                        
Alias           clear -> Clear-Host                                                                                                                                         
Alias           clhy -> Clear-History                                                                                                                                       
Alias           cli -> Clear-Item                                                                                                                                           
Alias           clp -> Clear-ItemProperty                                                                                                                                   
Alias           cls -> Clear-Host                                                                                                                                           
Alias           clv -> Clear-Variable                                                                                                                                       
Alias           cnsn -> Connect-PSSession                                                                                                                                   
Alias           compare -> Compare-Object                                                                                                                                   
Alias           copy -> Copy-Item                                                                                                                                           
Alias           cp -> Copy-Item                                                                                                                                             
Alias           cpi -> Copy-Item                                                                                                                                            
Alias           cpp -> Copy-ItemProperty                                                                                                                                    
Alias           curl -> Invoke-WebRequest                                                                                                                                   
Alias           cvpa -> Convert-Path                                                                                                                                        



No comments:

Post a Comment