PowerShell Profile Creation

PowerShell Logo

In the post PowerShell ISE Exchange session we have explored how it is possible to use a PowerShell profile to integrate Exchange sessions into PowerShell IDE, in this post we are going to explore what a PowerShell profile is and how it is created.

PowerShell Profile – What is it about?

Oversimplifying the matter a PowerShell profile is nothing else than a text file, yes that is not a typo, which contains PowerShell scripts/instructions and is saved with a .ps1 extension.

So why would you want to use a PowerShell profile? Simple make your life easier!  A PowerShell profile is executed automatically each time PowerShell is started so that you can have your scripts/instructions automatically run a set of instructions or even import modules which are not part of native PowerShell.

Let me give you an example of this, I often use PowerCli to manage VMware based infrastructures but find using standard shell very impractical for all but the most basic tasks so I’ve configured a PowerShell profile to automatically load VMware specific modules automatically each time I fire up ISE.

PowerShell Profile – Location, location, location

I’m not sure who coined this quote but you will beg with me if I borrow it for my post, as you probably have guessed PowerShell profile location is really important, but how you find out where the PowerShell profile is locate? Simple querying the $profile variable:

# Show PowerShell profile location

$profile

C:UsersLetheDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1

Fantastic we know where the PowerShell profile is located, to edit simply issue the following command:

# Open the default PowerShell profile

notepad $profile

If the file does not exist you will be asked to create a new one:

PowerShell Profile

There is a heads up here, if you run the above command from a server you will simply get a flat error like the following:

PowerShell Profile

If the above error message is displayed you can manually create the profile with the following command:

# Create the PowerShell profile file 

New-Item -path $profile -type file -force

Once the file has been created you can proceed and start filling it according to your needs.

PowerShell Profile – What goes in it?

As I said at the beginning of the post anything that you deem useful, I will show a couple of examples to show how versatile and useful a PowerShell profile can be, let’s start with something rather basic probably you’ve noticed PowerShell always starts the session in the same folder (usually your home folder) this is usually fine but what if you want PowerShell to start session in another folder let’s assume Z:Scripts? Nothing easier than adding the following to your PowerShell profile:

# Set PowerShell profile startup folder

Set-Location Z:Scripts

If you want PowerShell to greet you everytime you start a new session you can put something like this in your profile script:

# Greet the current user each time a new PowerShell session is started

Write-Host "Welcome back" ([Environment]::UserName)  -ForegroundColor Green

PS Z:Scripts> Welcome back HeloCheck

Of course the above goes on and there is a huge list of commands and/or customization you can apply to your PowerShell profile but let’s get back to the example at the beginning of the post, what if you want to load specific modules and/or snap-ins at each PowerShell startup? Here you go:

# Load PowerCli modules automatically at each startup

Add-PSSnapin VMware.VimAutomation.Core

# I find this module very useful to manage Active Directory so I load it in my profile

Add-PSSnapin Quest.ActiveRoles.ADManagement

PowerShell Profile – Execution Policy matters

The above should help you getting started but there are some things you need to keep in mind, as the title implies the first one is PowerShell Execution Policy if you’re running the default configuration (Restricted) external snap-ins and the PowerShell profile itself will fail to load so bear this in mind and set your execution policy accordingly.

Another factor to keep in mind is that PowerShell and ISE use different profiles as you can see in the $profile variable:

# PowerShell profile

C:UsersLetheDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1

# PowerShell ISE profile

C:UsersLetheDocumentsWindowsPowerShellMicrosoft.PowerShellISE_profile.ps1

 

Advertisements

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