0

I would like, from a powershell script, launch another powershell instance in administrator mode and, if possible, indicate to it some code to be executed in this same command line.

Here is the concrete case (I am using chocolatey for my package management under windows) :

Start-Process powershell -Verb runAs -ArgumentList "choco outdated"

This code works, but unfortunately the powershell window closes immediately after executing the code. So it is "impossible" to see outdated packages.

Do you have a solution for either:

  • freeze the script after the command?
  • or add more line of code to put Start-Sleep -Seconds 10?
  • or another solution?

Thanks in advance!

A-d-r-i
  • 31
  • 2
  • 11
  • `Read-Host` will interrupt and wait for user input – Mathias R. Jessen Aug 07 '20 at 13:37
  • Thank you for your answer ! Indeed I tried this: `Start-Process powershell -Verb runAs -ArgumentList "choco outdated","Read-Host" ` and this: `Start-Process powershell -Verb runAs -ArgumentList "choco outdated Read-Host"` unfortunately it does not work, after displaying outdated packages the window closes! – A-d-r-i Aug 07 '20 at 13:41
  • You can try this: `Start-Process powershell -Verb runAs -ArgumentList "-NoExit choco outdated"` – Adrian Ileana Aug 07 '20 at 13:59
  • It works perfectly too! Thank you ! – A-d-r-i Aug 07 '20 at 14:31

2 Answers2

0

Here's one way to get it to wait.

Start-Process powershell -Verb runAs -ArgumentList "choco outdated`n`rpause"
Doug Maurer
  • 8,090
  • 3
  • 12
  • 13
0

You can use the 'NoExit' argument: About PowerShell.exe

PowerShell[.exe]
    [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo]
    [-NoExit]
    [-Sta]
    [-Mta]
    [-NoProfile]
    [-NonInteractive]
    [-InputFormat {Text | XML}]
    [-OutputFormat {Text | XML}]
    [-WindowStyle <style>]
    [-EncodedCommand <Base64EncodedCommand>]
    [-ConfigurationName <string>]
    [-File - | <filePath> <args>]
    [-ExecutionPolicy <ExecutionPolicy>]
    [-Command - | { <script-block> [-args <arg-array>] }
                | { <string> [<CommandParameters>] } ]

PowerShell[.exe] -Help | -? | /?

Be sure to put the arguments in the '-ArgumentList' parameter in the correct order.

Start-Process powershell -Verb runAs -ArgumentList "-noexit", "-noprofile", "-command choco outdated"
  • Do you think it is possible to add several commands at the end? like this : `Start-Process powershell -Verb runAs -ArgumentList "-noexit", "-noprofile", "-command choco outdated", "-command choco upgrade"` – A-d-r-i Aug 08 '20 at 08:31