Share via

Why does PowerShell only close when it's elevated in certain ways?

Anonymous
2023-07-23T13:06:27+00:00

Hey! I been having a problem for a long time, which I had been using a work around for, but trying to circle back and fix this as I want to clean up how my script runs.

My script is a batch file which does some work and then runs a script in PowerShell

I'm looking to move this all to PowerShell, but what's the point if I can't overcome this obstacle, it's not worth converting the batch code to PowerShell. at that point.

Here's when it works and doesn't work:

Doesn't Work:

Right clicking batch file and running as admin

Having PowerShells #Requires -RunAsAdministrator

Having Command Prompt check if admin, and rerun itself if it's not using this code:

net session >nul 2>&1

if %errorlevel% neq 0 (

powershell.exe -Command "Start-Process '%~dpnx0' -Verb RunAs" 

exit 

)

Works:

Elevate Command Prompt, navigate and run the script

Elevate PowerShell, navigate and run the PowerShell portion of the script

If I skip the Batch file, remove #Requires -RunAsAdministrator, and run the PowerShell script itself - Although later this will fail due to lack of elevation

If I use this code to check if an administrator and rerun Powershell as an Admin

if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))

{

    Start-Process powershell.exe -Verb RunAs -ArgumentList "-File "$PSCommandPath""

    exit

}

I've tested this with even a simple script where it's just Read-Host but it closes immediately when getting to the PowerShell script, everything up until then executes perfectly

Looking this up online, people suggest execution policy, I use Bypass personally in my script when calling powershell or when the powershell command is elevated,

I have used the -NoExit switch to no avail

I have done the DISM and SFC download and scan with no errors or change after

I don't see why I would need to reinstall powershell - This happens on many machines I've tested with, it almost seems like a bug in PS 5.1

I don't want to use PS7 because this will be run on a machine with a limited network connection (in terms of speed) and so I don't want to download for this one time script it doesn't make sense to do that.

Has anyone else encountered this to this extreme? How did you resolve it? I'm desperate for a real solution

Windows for home | Windows 10 | Performance and system failures

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2023-07-24T02:37:16+00:00

    Well I ended up finding out some of the issue, I'd love for #Requires -RunAsAdministrator to work, but it seems I'll be sticking with the longer check for being admin and restating elevated if needed. Either way it seems that I'm not 100% sure what I did wrong,, but I can tell you that now it works after some messing around, but looks the same to me.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-07-24T01:51:12+00:00

    I don't understand...Powershell where? What do you mean where? I'm using 5.1 and on Windows 10 so it's only in one location...

    I did manage to find out the error which causes it to quit...Internal Windows PowerShell error. Loading managed Windows PowerShell failed with error 8009001d.

    Trying to see what I can find, but seems years ago this is claimed to have been fixed, but of course other issues then what the deal for that person was could cause this.

    Was this answer helpful?

    0 comments No comments
  3. MR_MEOW 3,541 Reputation points
    2023-07-23T13:57:56+00:00

    Here's a hint ; powershell where?

    cmd.exe powershell.exe

    all PowerShell is doing is calling cmd.exe

    In this PowerShell command you are doing

    cmd.exe net

    Possibly cmd.exe net1

    also you can run as user name via cmd.exe

    Learn your cmd.exe "command prompt" first.

    Also feel free to send a pull request on github / report an error.

    The biggest clue should be dp0 "dependency zero"

    Was this answer helpful?

    0 comments No comments