Powershell -Profiles-All About

sshot-2010-01-19-17-54-54

A Powershell profile is a script that eecutes when Powershell is first started. Profiles are used to cinfigure Powershell and load extra functionality automamatically at startup.

Profiles make the application of a standard, repeatable configuration much easier. There are four separate profiles in Powershell. IN order of loading, the profiles are:

  1. $pshome\prifle.ps1
  2. $pshome\Microsoft.Powershell_profile.ps1
  3. $home\My Documents\WindowsPowershell\profile.psb1
  4. $home\My Document\WindowPowerShell\Microsoft\profile.ps1

For frequent PowerShell users, the standard settings might not be ideal. We can change the settings of our PowerShell window to how we like it by modifying the profile.

The first thing we need to check is whether or not a profile already exists. We can do this by opening a PowerShell window and typing:

Test-Path $profile

 

Since our query returned “False” we are going to have to create a new profile. We’ll type the following in our PowerShell prompt:

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

sshot-2010-01-19-17-47-21

You should get the following pop-up, and just click yes to proceed.

sshot-2010-01-19-17-41-36

The profile script will now be created. It will be in the location following the “Directory:” output. When we browse to that location, the profile script will be there waiting for us to modify. Open the file named “Microsoft.Powershell_profile.ps1

 

The profile will be empty, so we can fill it with any commands that we want to run. You can set the starting prompt location to the root directory by adding:

set-location c:

We can change the title of the PowerShell window to SysadminGeek by adding:

$Shell.WindowTitle=”SysadminGeek”

We can also change the window size and scrollback with the following:

$Shell = $Host.UI.RawUI
$size = $Shell.WindowSize
$size.width=70
$size.height=25
$Shell.WindowSize = $size
$size = $Shell.BufferSize
$size.width=70
$size.height=5000
$Shell.BufferSize = $size

The background and text coloring can also be changed with these entries:

$shell.BackgroundColor = “Gray”
$shell.ForegroundColor = “Black”

You can put any scripts or aliases that you commonly use as well. We’ve included our alias forNotepad.exe

new-item alias:np -value C:WindowsSystem32notepad.exe

The final entry is included to clear everything from the PowerShell window, giving you a clean workspace:

Clear-Host

All together, it looks like this:

sshot-2010-01-19-19-45-59

After you’ve saved your profile, the next time you open PowerShell you will see all the changes made to your profile.

 

It’s pretty easy to customize your profile, and the more you use PowerShell, the more you will find yourself returning to add in new aliases and scripts that you need to run at startup

Summary of Powershell locations

All shells Standard PowerShell
All Users $pshome\profile.ps1 $pshome\ Microsoft.Powershell_profile.ps1
Idividual user ..\My Document\WindowsPowershell\Profile.ps1 ..\My Documents\

WindowsPowershell\Microsoft.PowerShell_profile.ps1.

 

$pshome is the path to the Powershell installation folder.. PowerShell doesnn’t create any profiles during the installation process.  All profiles have to be created manually. You can centralized Profile to reference share or unc path.

Profiles can perform several actions:

  • Loading additional functionality via PowerShell snapins
  • Creating functions and storing in memory for future use
  • setting the PowerShell prompt
  • Running scripts
  • Setting environmental factors such as color schemes
  • Changing the current folder.

A folder containing an example profile is created when Powershell is installed.  32 bit refernce is C:\Windows\Systems32\WindowsPowerShell\v(x)\

Microsoft.PowerShell_profile with Windows Explorer

Preparation:  Navigate to your Windows directory called,
Users
or
‘Documents and Settings’.
Now find a sub folder called: %username%\My Documents\WindowsPowerShell e.g.
C:\Users\YourName\My Documents\WindowsPowerShell
N.B. This folder is now called WindowsPowerShell (not PSConfiguration)

PowerShell Profile in Windows 7
PowerShell Microsoft.PowerShellISE_profile

Note 5:  Observe the above filenames: Microsoft.PowerShell_profile is for the command-line, while the GUI version has ISE in the filename.

PowerShel Profile.ps1

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

 

Reference
Richard Siddaway -Powershell In Practice


Leave a Reply

Your email address will not be published. Required fields are marked *