Share via


How to Configure and Use a PowerShell Profile

When you open a PowerShell window to perform some work all the aliases, functions and variables you create are only available in your current session.  If you exit the session all these changes are lost.  To retain these changes from session to session you can create a Windows PowerShell profile and customize it.  The profile is essentially a PowerShell script that runs when you start a PowerShell session.  You can copy your profile to other computers so you have a consistent environment on any computer you are working on.

Since the PowerShell profile is a script the Execution policy of your computer must allow scripts to run.  Open an elevated PowerShell window and run the following command to check your execution policy:

Get-ExecutionPolicy

If it says restricted then scripts are prevented from running.  To enable scripts run the following:

Set-ExecutionPolicy Remotesigned or Set-ExecutionPolicy Remotesigned Unrestricted

Now you are ready to configure a PowerShell profile.  At the PowerShell prompt type:

$Profile and press ENTER.

You should see something similar to the following:

C:\Users\muaddib\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Open the Documents folder and confirm there is a “WindowsPowerShel” folder.  If not create it.

Next open Notepad and create the “Microsoft.PowerShell_profile.ps1” profile file.

Now let’s add some content to the profile.  I have a variable that contains my DEV directory where I perform all my PowerShell work.  I also have a couple of aliases such as GH for “GET-HELP and C for CLS.   I also have a few functions I use frequently.  Here is an excerpt from my PowerShell profile.  When I was first learning PowerShell I used the banner shown below to remind me of some of the common commands every time I opened a PowerShell window.  Go ahead and add the content shown below to your profile (“Microsoft.PowerShell_profile.ps1”) for now to demonstrate the use of the profile and save the file,

# ALIAS Definitions
set-alias -name gh -value get-help
set-alias -name c -value cls

# FUNCTIONS
#get detailed help
Function GHD
{
CLS
Get-help $args[0] -detailed | more
} #END GHD Function

#Variables
$DEV = “C:\dev"

#change working folder to DEV folder
CD $DEV

#################################################################################
# BANNER
#################################################################################

Write-host -foregroundcolor green "USEFUL COMMANDS"
Write-host -foregroundcolor yellow " GET-HELP (Get-HELP ABOUT_*)"
Write-host -foregroundcolor yellow " GET-HELP GET-SERVICE –Detailed"
Write-host -foregroundcolor yellow " GET-COMMAND"
Write-host -foregroundcolor yellow " GET-MODULE –ListAvailable"
write-host ""

 

Now open a new PowerShell windows and you should see something similar to the figure below.

image

Now type GH GET-SERVICE and you should see the help for the GET-SERVICE cmdlets. 

Now type GHD GET-SERVICE to run the GHD function that will display detailed help for the GET-SERVICE cmdlet.  I find this easier than typing GET-HELP <cmdlets name > –detailed when I want detailed help.

So there you have it.  Once you have your profile customized its just a matter of moving the file to another computer so you have access to your common work environment.

Comments

  • Anonymous
    November 24, 2014
    Don, great stuff. Minor thing - you've got an type-o under your #banner section "GET-MMODULE" > "GET-MODULE"

  • Anonymous
    November 24, 2014
    @ Ilya B> - Corrected.  Thanks for the feedback.