Monday 14 September 2015

Creating Aliases


In the Last Post, I wrote a little about Aliases in PowerShell. So What about creating an Alias.

Yes we have the option for creating New Alias in PowerShell.And the cmldlet used for Creating aliases in PowerShell is New-Alias.

So, As Usuall, We Can Go For Get-Help on New-Alias cmdlet.

PS C:\> Get-Help New-Alias

NAME
    New-Alias
   
SYNOPSIS
    Creates a new alias.
   
   
SYNTAX
    New-Alias [-Name] <String> [-Value] <String> [-Description [<String>]] [-Force] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}]
    [-InformationVariable [<System.String>]] [-Option {None | ReadOnly | Constant | Private | AllScope | Unspecified}] [-PassThru] [-Scope [<String>]] [-Confirm] [-WhatIf]
    [<CommonParameters>]
   
   
DESCRIPTION
    The New-Alias cmdlet creates a new alias in the current Windows PowerShell session. Aliases created by using New-Alias are not saved after you exit the session or
    close Windows PowerShell. You can use the Export-Alias cmdlet to save your alias information to a file. You can later use Import-Alias to retrieve that saved alias
    information.
   
Let's look at the Syntax.

SYNTAX
    New-Alias [-Name] <String> [-Value] <String> [-Description [<String>]] [-Force] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}]
    [-InformationVariable [<System.String>]] [-Option {None | ReadOnly | Constant | Private | AllScope | Unspecified}] [-PassThru] [-Scope [<String>]] [-Confirm] [-WhatIf]
    [<CommonParameters>]

With the help of  this Syntax, Lets create an Alias for a cmdlet. Say Test-Connection.

Test-Connection cmdlet is used to Test connectivity to one or more remote computers.

PS C:\> New-Alias -Name TC -Value Test-Connection -Description 'Alias For Test-Connection cmdlet' 

Here,
-Name:- Name of the  cmdlet for which we have to create the Alias.

-Value:- Name of the alias for the cmdlet which we are creating alias.

-Description:- Description For the New alias of the cmdlet.

Lets check,

PS C:\> Get-Alias -Definition Test-Connection

CommandType     Name                                               Version    Source                                                                                        
-----------     ----                                               -------    ------                                                                                        
Alias           TC -> Test-Connection                                                 

Note: Since we Didn't mentioned Version and source info, Those are blank.

Lets Test The Connectivity to Loopback Adapter.
PS C:\WINDOWS\system32> Test-Connection 127.0.0.1

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
D-113037147   127.0.0.1       192.168.106.1    fe80::bc73:cd7e:5078:531b%15             32       0        
D-113037147   127.0.0.1       192.168.106.1    fe80::bc73:cd7e:5078:531b%15             32       0        
D-113037147   127.0.0.1       192.168.106.1    fe80::bc73:cd7e:5078:531b%15             32       0        
D-113037147   127.0.0.1       192.168.106.1    fe80::bc73:cd7e:5078:531b%15             32       0

Using Alias,

PS C:\WINDOWS\system32> TC 127.0.0.1

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
D-113037147   127.0.0.1       192.168.106.1    fe80::bc73:cd7e:5078:531b%15             32       0        
D-113037147   127.0.0.1       192.168.106.1    fe80::bc73:cd7e:5078:531b%15             32       0        
D-113037147   127.0.0.1       192.168.106.1    fe80::bc73:cd7e:5078:531b%15             32       0        
D-113037147   127.0.0.1       192.168.106.1    fe80::bc73:cd7e:5078:531b%15             32       0

We Have succesfully created an Alias for Test-connection cmdlet.

PS C:\WINDOWS\system32> Get-Alias -name TC|Select-Object Name,Definition,description

Name Definition      Description                     
---- ----------      -----------                     
TC   Test-Connection Alias For Test-Connection cmdlet





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