Configure PowerShell profile before next time you run PowerShell

A PowerShell profile in very simple terms, is a script that is auto-executed (read runs) whenever you start a PowerShell session. Why is this important? Suppose, you work on SharePoint administration for your daily bread and butter. Everytime, you would want to execute some cmdlets based on SharePoint, you would need to load PSSnapin for SharePoint. Same goes for Azure. Who would want to login everytime whenever you want to run some Azure cmdlets and go through that annoying pop-up box. It’s a good idea to have some commands auto-executed whenever you want to open PowerShell session. PowerShell profile allows you to configure your environment in advance. In this blog post, we’ll discuss how to configure a PowerShell profile.

The first thing before you configure profile, is to check if a PowerShell profile is already configured or not. This would be done by use of below command:

Test-Path $Profile

Here, $Profile is a system variable inside the PowerShell.

If the output of this command is False, it means that you can go ahead and configure a profile. If it’s true, it means that you would need to edit already existing profile.

Here’s a sample output from my machine:

check-if-powershell-profile-is-already-configured
Check if PowerShell profile is already configured

You would note that output of $Profile is a path on the local system. This is the default location for profile script.

Now, let’s create a $Profile script for us by using New-Item cmdlet:

New-Item $Profile -ItemType file –Force

This would create the required path for profile:

create-powershell-profile-path
Create PowerShell profile path

Since, we prefer commands than GUI, rather than going to directory location and editing the file, we would simply run below command:

notepad $profile

It will open the file in notepad for us to edit:

edit-powershell-profile-using-notepad
Edit PowerShell profile using notepad

Now, let’s add some code to profile:

# 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

Set-Location C:\

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 ""

Hit Ctrl+S to save it. Now, let’s close existing window and open a new PowerShell session to see PowerShell profile in action:

see-powershell-profile-in-action
See PowerShell profile in action

In one of the previous blog post we discussed how to create persistant Azure PowerShell logins. There we described commands to do that but did not explained, how to create/edit PowerShell profile. Hopefully, this clears air.

2 thoughts on “Configure PowerShell profile before next time you run PowerShell

  1. Thanks for one’s marvelous posting! I seriously enjoyed reading it, you will be a great author.I will remember to bookmark your blog and definitely will come back at some point. I want to encourage yourself to continue your great posts, have a nice afternoon!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s