Start-Process with -Verb RunAs and SysNative

Joel Kraft 1 Reputation point
2024-12-12T03:30:01.3033333+00:00

I'm working on a script where I want to make sure that I am always running in an elevated 64-bit instance of PowerShell.

Given these variables:

$PsPath = "$env:SystemRoot\SysNative\WindowsPowerShell\v1.0\powershell.exe"
$PsArgs = '-ExecutionPolicy', 'Bypass', '-File', $PSCommandPath

Is there any reason why trying to start the process would error out? This is starting from a non-elevated, 32-bit PowerShell process.

PS D:\Intune\Scripts> Start-Process -FilePath $PsPath -Verb RunAs -ArgumentList $PsArgs
Start-Process : This command cannot be run due to the error: The system cannot find the path specified.
At line:1 char:1
+ Start-Process -FilePath $PsPath -Verb RunAs -ArgumentList $PsArgs
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

If I remove "-Verb RunAs", I get a 64-bit instance, but it is not elevated.

If I use System32 instead of SysNative, I will get an elevated 32-bit instance.

This limitation is not making a lot of sense to me, so any insight is appreciated!

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2024-12-12T20:03:28.83+00:00

    The SysNative is a virtual directory that doesn't exist when running a 32-bit process.


  2. MotoX80 36,291 Reputation points
    2024-12-13T13:31:54+00:00

    This works interactively. I don't know about Intune though.

    "Command path is $PSCommandPath"
    if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
        "Running as 32, launching 64 bit instance."
    	$PsPath = "$env:SystemRoot\SysNative\WindowsPowerShell\v1.0\powershell.exe"
    	$PsArgs = '-ExecutionPolicy', 'Bypass', '-File', $PSCommandPath
    	Start-Process -FilePath $PsPath -ArgumentList $PsArgs
    	Start-Sleep -seconds 10
    	return
    }	
    "We are running x64."
    if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") -eq $false) {
    	"We are not elevated."
    	$PsPath = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
    	$PsArgs = '-ExecutionPolicy', 'Bypass', '-File', $PSCommandPath
    	Start-Process -FilePath $PsPath -ArgumentList $PsArgs -Verb RunAs 
    	Start-Sleep -seconds 10
    	return
    }	
    "We are good to go."
    start-sleep -seconds 10 
    return 
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.