locked
Running script elevation in background RRS feed

  • Question

  • # Get the ID and security principal of the current user account
    $myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
    $myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
    
    # Get the security principal for the Administrator role
    $adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
    
    # Check to see if we are currently running "as Administrator"
    if ($myWindowsPrincipal.IsInRole($adminRole))
       {
       # We are running "as Administrator" - so change the title and background color to indicate this
       $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
       $Host.UI.RawUI.BackgroundColor = "DarkBlue"
       clear-host
       }
    else
       {
       # We are not running "as Administrator" - so relaunch as administrator
       
       # Create a new process object that starts PowerShell
       $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
       
       # Specify the current script path and name as a parameter
       $newProcess.Arguments = $myInvocation.MyCommand.Definition;
       
       # Indicate that the process should be elevated
       $newProcess.Verb = "runas";
       
       # Start the new process
       [System.Diagnostics.Process]::Start($newProcess);
       
       # Exit from the current, unelevated, process
       exit
       }
    
    # Run your code that needs to be elevated here
    Get-AppXPackage -AllUsers -Name Microsoft.MicrosoftEdgeDevToolsPreview | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register “$($_.InstallLocation)\AppXManifest.xml” -Verbose}
    Add-AppxPackage -register “C:\windows\SystemApps\Microsoft.MicrosoftEdgeDevToolsClient_8wekyb3d8bbwe\AppxManifest.xml” -DisableDevelopmentMode -Confirm:$false
    Write-Host -NoNewLine "Press any key to continue..."
    $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    
    #Can I add  powershell.exe -nologo -command  in place of   $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";


    • Edited by eorosz Monday, July 22, 2019 4:09 PM
    Monday, July 22, 2019 4:07 PM

All replies

  • When I try running this I get the following errors 

    Get-AppXPackage : The term 'Get-AppXPackage' is not recognized as the name of a cmdlet, 
    function, script file, or operable program. Check the spelling of the name, or if a path 
    was included, verify that the path is correct and try again.
    At line:37 char:1
    + Get-AppXPackage -AllUsers -Name Microsoft.MicrosoftEdgeDevToolsPrevie ...
    + ~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Get-AppXPackage:String) [], CommandNotFoundE 
       xception
        + FullyQualifiedErrorId : CommandNotFoundException
     
    Add-AppxPackage : The term 'Add-AppxPackage' is not recognized as the name of a cmdlet, 
    function, script file, or operable program. Check the spelling of the name, or if a path 
    was included, verify that the path is correct and try again.
    At line:38 char:1
    + Add-AppxPackage -register “C:\windows\SystemApps\Microsoft.MicrosoftE ...
    + ~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Add-AppxPackage:String) [], CommandNotFoundE 
       xception
        + FullyQualifiedErrorId : CommandNotFoundException
     
    Press any key to continue...Exception calling "ReadKey" with "1" argument(s): "The method or 
    operation is not 
    implemented."
    At line:40 char:1
    + $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : NotImplementedException


    Monday, July 22, 2019 4:11 PM
  • That command is only available on Windows 10 an later. (2016 later)


    \_(ツ)_/


    Monday, July 22, 2019 10:11 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Wednesday, July 31, 2019 7:06 AM