Skipping Profile on Keystroke

by Jun 16, 2014

Maybe you'd like to be able to occasionally skip certain parts of your profile script. For example, in the ISE editor, simply add this construction to your profile script (path to your profile script is found in $profile, it may not yet exist):

if([System.Windows.Input.Keyboard]::IsKeyDown('Ctrl')) { return }

This will skip all remaining lines in your profile script if you hold CTRL while launching the ISE editor.

Or, you can use it like this:

if([System.Windows.Input.Keyboard]::IsKeyDown('Ctrl') -eq $false) 
{ 
  Write-Warning 'You DID NOT press CTRL, so I could execute things here.'
 
}

This would run the code in the braces section only if you did not hold down CTRL while launching the ISE.

If you want to use the code in the PowerShell console, too, then make sure you load the appropriate assembly. This would work in all profile files:

Add-Type -AssemblyName PresentationFramework
if([System.Windows.Input.Keyboard]::IsKeyDown('Ctrl') -eq $false) 
{ 
  Write-Warning 'You DID NOT press CTRL, so I could execute things here.'
 
}

Twitter This Tip! ReTweet this Tip!