Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to return to the command prompt temporarily

How would One allow users to "Pause the current Pipeline and return to the command prompt" and later resume in a powershell script?

I stumbled upon this line in a blog post about User Interaction in Powershell

$suspend = New-Object System.Management.Automation.Host.ChoiceDescription "&Suspend", "Pause the current pipeline and return to the command prompt. Type ""exit"" to resume the pipeline."

It was a mock option in a prompt imitating the appearance of a native command (Remove-Item). Lo and behold: That command actually Implements that behavior. Doing a quick Google Search, I did not find anyone trying to implement this in a script.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use $Host.EnterNestedPrompt() to suspend the current operation to enter a "nested" prompt – execution will resume once you exit it (using either exit or $Host.ExitNestedPrompt()):

function f {
  param([switch]$Intervene)

  $abc = 123

  if($Intervene.IsPresent){
    $host.EnterNestedPrompt()
  }

  Write-Host "`$abc has value '$abc'"
}

Now try invoking the function with and without the -Intervene switch:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading