• 5. User-Defined Powershell Aliases using Powershell Profiles

    Posted on February 26, 2013 by in Desktop Engineering, Powershell, Scripting

    User-Defined Aliases using PowerShell Profiles

    A Profile is script that runs at startup of the session. The location of the Profile is stored in the $Profile variable, which by default is “My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1″

    The Windows PowerShell profile is simply a script file that runs when Windows PowerShell starts up. You can put cmdlets, scripts, functions – any valid Windows PowerShell commands – into this script file. Each time you start Windows PowerShell, this script file will run. That means you can use the profile to set up your Windows PowerShell environment. Typically that would be custom console settings and aliases, but use your imagination and you can come up with other things you’d like to customize in PowerShell before you start working with it. 

    To verify the location of your profile type:

     $Profile <enter> 

    The Profile is a script that is denoted by “.PS1” extension. All Powershell scripts are identified by this extension. You can use powershell editor or notepad to edit the powershell script. Before editing the script file check the powershell execution Policy.

    When PowerShell is first installed the default execution policy is “Restricted.” This means PowerShell will not run scripts or configuration files.

    Below is the list of policy levels:

    • Restricted – All Scripts are disabled and wont be run.
    • AllSigned – All scripts and configuration files must be signed by a trusted publisher.
    • RemoteSigned – All scripts and configuration files downloaded from the Internet must be signed by a trusted publisher.
    • Unrestricted – All scripts and configuration files will run. Scripts downloaded from the Internet will prompt for permission before running.

    Use the “Get-ExecutionPolicy” cmdlet to verify policy level:

     Get-ExecutionPolicy <enter> 

    This command will display the current powershell policy that is set.

    To change the policy to “Unrestricted”

     Set-ExecutionPolicy Unrestricted <enter> 

    Verify the policy is set  Unrestricted by typing

     Get-ExecutionPolicy <enter> 


    The Policy is set to run the scripts.  Follow the steps to create the profile.

    1. Verify if the Profile exists:
       test-path $Profile <enter> 

      If the result=FALSE then NO Profile Exists
      If the result=True then Profile already Exists

    2. Create a new Profile:
       New-Item -Path $Profile -ItemType file -Force <enter> 
    3. Verify new Profile was created
       test-path $Profile <enter> 

    4. Edit the Profile .PS1 file and customize it.

       Notepad $Profile <enter> 

      Microsoft.PowerShell_profile.ps1 file opens up in notepad. Add the customizaitons in the file, save and close it.

    After the profile is saved. Exit the powershell session.

    Open a new session and verify the Alias

     gs <enter> 

    The alias will start working once the profile is loaded successfully. Also verify the new alias by typing the command

     get-alias <enter> 


    Be Sociable, Share!

    Written by

    Senior Desktop/Infrastructure Engineer with over 10 years of proven experience in planning,designing & implementation of enterprise level Workstation builds for Finance, Pharmaceuticals, Telecommunication and other Domains. Desktop Engineer cannot rest as, Desktop world is very different it is fast paced, new technologies evolve and change at a rapid pace. For more info please visit: www.linkedin.com/in/srinivaskolla

    View all articles by

    Email : [email protected]

    Leave a Reply