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





No comments:

Post a Comment