Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bypass the personal and system profiles if the Shift key is hold down. #20435

Open
iRon7 opened this issue Oct 4, 2023 · 5 comments
Open

Bypass the personal and system profiles if the Shift key is hold down. #20435

iRon7 opened this issue Oct 4, 2023 · 5 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug WG-Interactive-Console the console experience WG-NeedsReview Needs a review by the labeled Working Group

Comments

@iRon7
Copy link

iRon7 commented Oct 4, 2023

Summary of the new feature / enhancement

PowerShell startup problems are often caused by the $Profile script as e.g.: Powershell 7 Blank, yet troubleshooting requires to open a different (none-PowerShell) prompt or playing with the shortcuts of the Operating System.
Probably the easiest way (from Windows) is Start --> Run --> pwsh -NoProfile.
For an end-user, this isn't always that obvious as the $Profile script might also cause less clear issues (e.g. different host colors than expected). Besides, instructions might be limited to the Run with PowerShell context-menu or a locked-down system with a single custom shortcut.

Proposed technical implementation details (optional)

Bypass the personal and system profiles while holding down the Shift (or any other control) key.

By adding the following code in the top of my $Profile, I simulated this propose.

 Add-Type -Name Keyboard -Namespace GetKeyState -MemberDefinition @'
    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] 
    public static extern short GetAsyncKeyState(int virtualKeyCode); 
'@
if ([GetKeyState.Keyboard]::GetAsyncKeyState(16) -eq -32767) { return 'bypassed PowerShell Profile' }

But as other (unwanted) scripts might simply replace the complete $Profile script, it should actually be managed by the PowerShell engine.

@iRon7 iRon7 added Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. labels Oct 4, 2023
@237dmitry
Copy link

For an end-user, this isn't always that obvious as the $Profile script might also cause less clear issues (e.g. different host colors than expected)

I don't understand how this can be implemented with all the variety of launch methods. For example, I run the pwsh profile in the Windows Terminal with Ctrl-Shift-2 and will I have to hold down another Shift with my other hand? The same story with launching elevated pwsh from lnk-file with Ctrl-Shift+click

I believe that end-users can run pwsh -nop

@iRon7
Copy link
Author

iRon7 commented Oct 4, 2023

@237dmitry,

I don't understand how this can be implemented with all the variety of launch methods.

It not supposed to be a solution for any CLI (as e.g. #8072).
But when launched (in anyway) from any UI, as e.g. a (pinned) shortcut in Windows.

The same story with launching elevated pwsh from lnk-file with Ctrl-Shift+click

Maybe you have a point here (the Shift control key might not be the best choice), but in the idea, you might do a right-click on the lnk-file and select Run as Administrator while holding down the Shift key.

@kilasuit kilasuit added the WG-Interactive-Console the console experience label Oct 4, 2023
@jhoneill
Copy link

jhoneill commented Oct 5, 2023

I was going to post "You can put this in your profile to skip it if shift is held down" but you have already found that :-)
I found that myself because it seemed way back (V2 or V3) that this should be built in and wasn't - starting in "safe" mode by holding down shift (or sometimes ctrl) goes back decades and I've never had an explanation for why PowerShell can't.

Anyone can run pwsh -noprofile - some of us even create shortcuts for that (I have one in terminal) but it would save enough people a few seconds often enough that I'd add it

@kilasuit
Copy link
Collaborator

kilasuit commented Oct 5, 2023

Probably the easiest way (from Windows) is Start --> Run --> pwsh -NoProfile.

in a session you can relaunch pwsh but for your usecase would this in windows be of use? (been reviewed but if we can get more yes'es then it can become something that you get outta the box for free) #18148

@kilasuit kilasuit added WG-NeedsReview Needs a review by the labeled Working Group and removed Needs-Triage The issue is new and needs to be triaged by a work group. labels Oct 5, 2023
@iRon7
Copy link
Author

iRon7 commented Oct 5, 2023

@kilasuit

in a session you can relaunch pwsh

In what session? What if I can't open any new pwsh session anymore as it is broken due to $Profile?
In Windows, I know my way around (I would probably open CMD if available) but in other Operating Systems, I feel less secure to go (or guide somebody) through a repair process unless I would still be able to open PowerShell (in "safe mode").
In other words:
If PowerShell doesn't start anymore, a very likely cause is the $Profile.
To resolve this, the instructions would be some something like (Maybe something for about Profiles):

  1. Open PowerShell while holding down the Shift key
  2. Rename the each $Profile:
$Profile.PSObject.Properties | Where-Object value -Like *profile.ps1 | ForEach-Object {
    Rename-Item $_.Value "$($_.Value).Bak" -Force -ErrorAction SilentlyContinue
}
  • Note that the renaming of some of these profiles requires administrator rights.

These instructions will probably get a lot more complicated (OS/Console/Terminal specific and taking specific configurations in consideration) when there isn't any pwsh session available anymore...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug WG-Interactive-Console the console experience WG-NeedsReview Needs a review by the labeled Working Group
Projects
None yet
Development

No branches or pull requests

4 participants