PowerShell Version Profile Tips

Now that PowerShell v3 is finally and officially with us, I’m trusting that many of you are embracing it. I’ve made the jump, although I have had to make some minor adjustments. If you are going to live entirely in a PowerShell v3 world, fantastic! Go forth and spread the news. But perhaps you are like me where there are still times you need to use PowerShell v2. Because of the material I develop, I often need to fall back to PowerShell v2. Fortunately this is easily done.

powershell version 2.0

This assumes you have PowerShell 2 enabled as a feature which Windows 8 has by default. But that is just the start. I use PowerShell profile scripts extensively. When I start a v2 session, my profile scripts run. The problem is when they hit commands that are specific to v3. I could run my v2 session with -noprofile, but that defeats the purpose. I’ll come back to this in a moment. The first tweak I have made is to define a function in my profile script for the current user current host (ie the console) to automatically start a PowerShell v2 session. This way, whenever I start PowerShell, I automatically get a v2 session started.

Function NewV2Session {


if ($NoProfile) {
StartProcess -file PowerShell.exe arg ‘-version 2.0 -nologo -noprofile’

else {
StartProcess -file PowerShell.exe arg ‘-version 2.0 -nologo’

} #end function

I can also use this function to launch future sessions with or without profiles. But I still need to manage both versions in the same profile script. Back to currentuser/currenthost, I have this code:

#kick off a clean v2 session if running v3
if ($psversiontable.psversion -eq "3.0") {
Else {
#add v2 specific code
#set colors
} #else v2

When the v2 session is kicked off and the profile runs again, I change the colors of the v2 session to distinguish it. I could also insert any other v2 specific commands in the Else script block. For that matter, I could also insert any other v3 specific commands in the If script block. This is what I have done with another profile script, the one for current user/all hosts.

if ($psversiontable.psversion -eq "3.0") {
#run v3 specific commands
#update PowerShell help
. c:\scripts\UpdatePowerShellHelp.ps1

$PSDefaultParameterValues[email protected]{
"Send-MailMessage:From"="[email protected]"
"Send-MailMessage:To"="[email protected]"

} #if v3

This is code that runs whenever I start the console or the ISE. I don’t have any v2 specific commands but I am taking advantage of default parameter values. If the references to the updating help command intrigue you, take a look at an article I published on the Petri Knowledgebase.

The end result of all of this is I get properly setup sessions based on the PowerShell version. You may not need to use all the profile scripts that I am, but if you need to share profiles between versions, learn how to use $psversiontable.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati