Configuration
PowerShell is very configurable. There are a number of possible ways to configure your PowerShell sessions including:
- Profiles
- Console Files
- Environmental variables
- Shell Properties
Profiles
In order for profiles to be executed the PowerShell execution policy must be configured correctly. See Execution Policy in this guide for further details Profiles are in effect PowerShell scripts that are executed when a PowerShell console is opened. They are given a .ps1 extension the same as scripts. It is possible for a Powershell console to be affected by up to four separate profiles at start up. In order of execution these profiles are:
- %windir%\system32\WindowsPowerShell\v1.0\profile.ps1 which applies to all users and all shells
- %windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1 which applies to all users, but only to the Microsoft.PowerShell shell
- %UserProfile%\My Documents\WindowsPowerShell\profile.ps1 which applies only to the to the current user, but affects all shells
- %UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 which applies only to the current user and the Microsoft.PowerShell shell
These locations are for a 32-bit Windows XP or Windows Server 2003 machine. On a Vista install My Documents is replaced by Documents. On a 64-bit machine system32 is replaced by syswow64.
%UserProfile and %windir% are environmental variables. Environmantal variables will be discusued later but for now the values of these particular environmental variables can be found by using the Get-Item cmdlet.
PS> Get-Item env:windir
Name Value
---- -----
windir C:\WINDOWS
PS> Get-Item env:userprofile
Name Value
---- -----
USERPROFILE C:\Documents and Settings\Richard
The Microsoft.PowerShell is the default PowerShell as installed by the download. The number profiles can be extended further in that if you create your own shell using the makeshell utility in the SDK it is possible to have named profiles associated with those shells. This is an advanced topic that will not be covered further here.
Having four possibilities for profiles can lead to some confusion as to which profile to use especially as if a function is redefined in a later profile it will over write the earlier definition. This behaviour can be contolled to a certain extent as is discussed in the following blog post.
http://blogs.msdn.com/powershell/archive/2007/01/25/controlling-powershell-function-re-definition.aspx It is generally better to only define the function once rather ensuring that it will not be over written. Unless there is a specific reason for using any of the other profiles I tend to prefer putting all profile entries into the %UserProfile%\My Documents\WindowsPowerShell\profile.ps1 profile. This way I know that it will apply to all shells that I start on my machine. On a server where multiple users may be accessing PowerShell it would be better to have a single profile affecting all users in which case use %windir%\system32\WindowsPowerShell\v1.0\profile.ps1
Having created our profiles what are we going to put in them? The simple answer is anything that you need to configure your PowerShell environment to behave the way you want it to. This can include:
- Function definitions
- Aliases for existing cmdlets
- Snapins and providers to load
- Aliases for executables such as installutil.exe which is used to register snapins
- Changing the prompt - about which there is much discussion
- Set environemntal variables including colors
$host will show some information about the current shell
PS> $host
Name : ConsoleHost
Version : 1.0.0.0
InstanceId : 9176e99b-0179-483a-a4da-81d465300a03
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-GB
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
$host.privatedata can be accessed to view and set the colors used in the PowerShell display window.
PS> $host.privatedata
ErrorForegroundColor : Red
ErrorBackgroundColor : White
WarningForegroundColor : Yellow
WarningBackgroundColor : Black
DebugForegroundColor : Yellow
DebugBackgroundColor : Black
VerboseForegroundColor : Yellow
VerboseBackgroundColor : Black
ProgressForegroundColor : Yellow
ProgressBackgroundColor : DarkCyan
PS>
To set particular colors use
$host.privatedata.ErrorBackgroundColor = "White"
It is not necessary for all of these setting to be in the profile script. It is possible for the profile to call other scripts during execution which can then contain the functionality you need. In this way it is possible to centralise the profile maintenance if required.
A very good example profile is included with the PowerShell Community Extensions that can be downloaded from here -
http://www.codeplex.com/Wiki/View.aspx?ProjectName=PowerShellCX
Console Files
Console files – are xml files that have an extension .psc1. They store configuration settings for a particular console. For example this is a console file produced from my current configuration
<?xml version="1.0" encoding="utf-8"?>
<PSConsoleFile ConsoleSchemaVersion="1.0">
<PSVersion>1.0</PSVersion>
<PSSnapIns>
<PSSnapIn Name="Pscx" />
</PSSnapIns>
</PSConsoleFile>
This adds the Snapin for the PowerShell Community Extensions to my default PowerShell configuration.
A console file can be produced by use the following cmdlet –
Export-Console filepath_for_console_file.psc1
Full details of Export-Console can be found by using -
Get-Help Export-Console -full
Ok so this is all well and good but what does it mean for us in practice. If you have installed the Exchange 2007 Administration Tools you will know that a specific PowerShell console is available from the Start Menu. This console consists of the base PowerShell console with the Exchange Management Shell Snapin layered on top. If you look at the command used to start this shell you can see that it uses a PowerShell Console file for the configuration. The command is:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\PowerShell.exe
-PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1"
-noexit
-command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'"
As you can see it starts a normal PowerShell console but adds in the extras via a PowerShell Console file. This explains why when you install Exchange 2007 PowerShell is a pre-requisite but after the installation you have access to pure PowerShell and the Exchange Management Shell that contains the additional Exchange specific PowerShell commands. I don’t particularly like the idea of having multiple versions of PowerShell available on a machine. It’s just too confusing. I want a single version of PowerShell on a machine that contains all of the commands I need to administer that environment. This situation will become more complicated as further products are released that utilise PowerShell.
The console file shows us which snapin is being loaded to create the customised shell. If you then go to the basic PowerShell console and use
Get-PSSnapin –registered
It will show you the Snapins that are available. All Snapins on a machine are available to all consoles though they might not be installed in a particular console. You can then use -
Get-PSSnapin snapin_name
Which will load the particular snapin you need. This enables you to create a single console with all of the functionality you need. You will also need to check any scripts that run when a customised console such as Exchange starts up and copy any needed parts into your profile.
Environmental variables
Environmental variables carry the same meaning as those in DOS. It is not possible to access the environmental variables by %variable_name% as in DOS batch files. To view the current environmental variables use
PS> get-item env:
Name Value
---- -----
Path C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM;C:\Program Files\Microsoft SQ...
TEMP C:\Temp
SESSIONNAME Console
PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PSC1
USERDOMAIN HOME04
PROCESSOR_ARCHITECTURE x86
SystemDrive C:
APPDATA C:\Documents and Settings\Richard\Application Data
windir C:\WINDOWS
SonicCentral C:\Program Files\Common Files\Sonic Shared\Sonic Central\
TMP C:\Temp
USERPROFILE C:\Documents and Settings\Richard
ProgramFiles C:\Program Files
FP_NO_HOST_CHECK NO
HOMEPATH \Documents and Settings\Richard
COMPUTERNAME HOME04
USERNAME Richard
NUMBER_OF_PROCESSORS 1
PROCESSOR_IDENTIFIER x86 Family 6 Model 13 Stepping 8, GenuineIntel
SystemRoot C:\WINDOWS
ComSpec C:\WINDOWS\system32\cmd.exe
LOGONSERVER \\HOME04
myvirtualmachines c:\VirtualMachines
CommonProgramFiles C:\Program Files\Common Files
PROCESSOR_LEVEL 6
PROCESSOR_REVISION 0d08
VS80COMNTOOLS C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\
lib C:\Program Files\SQLXML 4.0\bin\
ALLUSERSPROFILE C:\Documents and Settings\All Users
PscxHome C:\Program Files\PowerShell Community Extensions
OS Windows_NT
HOMEDRIVE C:
To get the value of a specific environmental variable use
PS> get-item env:windir
Name Value
---- -----
windir C:\WINDOWS
or use the PowerShell
PS> $env:windir
C:\WINDOWS
PS>
To create a new environmental variable use
PS> new-item env:test -value "test"
Name Value
---- -----
test test
Note that this will not persist the environmental variable between sessions. You need to use the normal Windows methods of setting environmental variables to create a persistant variable or you could set it at startup by putting the definition into your profile.
To change the value of an environmental variable use
PS> set-item env:test -value "BIG Test"
PS> $env:test
BIG Test
PS>
or
PS> $env:test = "HUGE TEST"
PS> $env:test
HUGE TEST
PS>
Shell Properties
Finally it is possible to alter the physical properties of the window PowerShell is displayed in. As for a cmd prompt right click the PowerShell icon in the top left corner of the Window and select Properties. The colors, fonts and layout can be controlled for the current window or all windows created from the short cut.
It is possible to configure PowerShell in many ways including:
- Profiles
- Console Files
- Environmental variables
- Shell Properties
The methods shown above will allow you to create a PowerShell environment that is configured exactly as you need it.