PowerShell


PowerShell Scripting  - Profile.ps1

Introduction to Windows PowerShell's Profile.ps1

Old timers like my friend 'Barking' Eddie can remember configuration files such as AutoExec.bat and Config.sys.  More modern Microsoft operating systems need boot.ini; all these files control the startup environment.  Well the purpose of this page is to configure the equivalent start-up file PowerShell namely: profile.ps1. 

Note 1: The extension is ps, followed by numeric one, making ps1 (not psl).
Note 2: The name of this file is 'profile' - singular

PowerShell Profile.ps1 Topics

Mission to enable a basic Profile.ps1 file

When I installed my Windows PowerShell v 1.0 it did not configure the profile.ps1 file automatically.  A little research revealed that the key folder is called 'WindowsPowerShell'.  I want to spare you my grief in troubleshooting profile.ps1 and instead, I want to explain how, and where, you can control this configuration file.  Remember that the purpose of profile.ps1 is to control the initial settings, for example, set the working directory and add PowerShell snap-ins.

Enabling PowerShell Scripts

Profiles.ps1 is a script file; by default you cannot run any scripts, this is because at install the executionpolicy is set to Restricted.  Without making the following configuration change you may get an error message:  'The execution of scripts is disabled'.  Incidentally, this is the same command that allows all PowerShell’s scripts to run. My point is that you may have run this command just before you configured your first cmdlet scripts.

The best solution is to employ PowerShell's very own commands to manipulate the registry:

set-executionpolicy Unrestricted.

Or to be more secure:

set-executionpolicy RemoteSigned

Note 1: Investigate with set-executionpolicy -?

PowerShell Registry

Alternatively, you could change a registry value called REG_SZ ExecutionPolicy from Restricted to UNRestricted (or better RemoteSigned).  Here is the full path to enable .PowerShell cmdlets in general and profile.ps1 in particular.

HKLM\Software\Microsoft\PowerShell\1\ShellIds \Microsoft.Management.Automation.PowerShell

Locating the path for the Profile.ps1 file

My main problem with configuring profile.ps1 was locating the precise path shown below.  In truth, it was my fault for not paying attention to detail.  Note: as with all PowerShell nouns, profile.ps1 is the singular 'profile' and not the plural 'profileS'.

Preparation:  Navigate to your Windows directory called, 'Documents and Settings'.

First Task: Find a sub folder called: %username%\My Documents\WindowsPowerShell e.g.
C:\Documents and Settings\YourName\My Documents\WindowsPowerShell
N.B. In version 1.0 (and RC2) This folder is now called WindowsPowerShell (not PSConfiguration)

Locating the path for Profile.ps1. C:\Documents and Settings\All Users\Documents\PSConfiguration\profile.ps1

Second Task:  Launch notepad and create a file called precisely profile.ps1.  Make sure that you have the correct file extension, .ps1 and not .txt.  This is the position; you should now have see the file and folder in the above screen shot.

C:\Documents and Settings\%username%\My Documents\WindowsPowerShell\profile.ps1

Third Task:  In the file called profile.ps1, add instructions to configure your command shell.  I added a phrase to prove that it's my profile.ps1 which is active and not the default profile.ps1.  "You are now.....".  PowerShell also supports the $env:Username variable.

set-location d:\powershell
cls

# welcome message
"You are now entering PowerShell : " + $env:Username

Fourth Task:  Now launch PowerShell.PowerShell Profile Command Line

Once you have succeeded in placing your profile.ps1 in the correct path, then you should see a similar command line to the screen shot to the right.

Note: As ever, my mission is to get you started, however, if you are interested, you can research more flashy commands to enter into this startup configuration file: profile.ps1.

Alternative Locations for Profile.ps1

It is possible to configure the profile.ps1 at other locations:

%windir%\system32\WindowsPowerShell\v1.0\profile.ps1
The above profile applies to all users and all shells.

%windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1
The above profile applies to all users, but only to the Microsoft.PowerShell shell.

As you see, one of the commands I used in profile.ps1 was cls.  Actually, cls is an Alias for clear-host.  Time to find out more about PowerShell's Alias?

  ˚

Summary of Windows PowerShell Profile.ps1

As you are likely to spend a great deal of time at the PowerShell command line, it makes sense to spend time configuring the startup folder and appearance of shell.  The first step is to locate the location(s) where the key file called profile.ps1 is kept.  Once you have found the correct folder, then you can add instructions to the profile.ps1 file.

See Also

Windows PowerShell Home  • Introduction  • Cmdlets  • Exchange 2007  • Profile.ps1  • $_.Pipeline

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

 *


Google

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.