Backwards Incompatibility: Slip Into Something Just As Comfortable
PowerShell is designed to be backwards compatible.
However, there are some things that you can do in v3 and above that just don't work in v2. This is deliberate.
And, there's often the need to write code for systems running v2.
So, even in Windows 10, which ships with v5, we give you the ability to drop back into v2:
Nice.
Now, the other day I came across an issue with a couple of the functions I load into memory via my console host's $Profile script. They worked in v2, v3 and v4, but not in v5. I use these functions to demonstrate some of the cool things you can do with PowerShell to my students.
All was not lost, I wrote another function that drops me back into v2. This allows these problematic functions to run again AND gives me very easy access to a test platform. Have a look at this:
Notice how the version information changes once the function is executed. Also notice how the prompt changes - this is just to remind me that I'm in v2.
Here's the Start-VersionTwo function:
Function Start-VersionTwo {
PowerShell.exe -Version 2 -NoProfile -NoExit -Command {
#Make the prompt v2 specific
Function Prompt {
Return ("PSv2 " + $(Get-Location) + ">")
}
#Import the screensaver module
Import-Module ScreenSaver
#Lee Holmes' Burn Console script
Function Start-BurnConsole {
cd "C:\Users\ianfarr\OneDrive - Microsoft\Scripts\_PS1\Burn Console"
.\burn.ps1
}
}
}
The interesting bit here is that the function calls PowerShell.exe with 2 supplied to the version parameter. We also say not to run my profile script, remain open and then execute a script block. The script block loads a couple of functions and a module into memory, allowing me to, once again, perform my 'demonstrations'.
Sweet.
Comments
- Anonymous
October 02, 2015
very cool