Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9174381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:44:49+00:00 2026-06-17T16:44:49+00:00

I have the following powershell script that I am trying to adapt for use

  • 0

I have the following powershell script that I am trying to adapt for use within a larger script.

function New-Choice {
<#       .SYNOPSIS
                The New-Choice function is used to provide extended control to a script author who writing code
        that will prompt a user for information.
        .PARAMETER  Choices
                An Array of Choices, ie Yes, No and Maybe
        .PARAMETER  Caption
                Caption to present to end user
        .EXAMPLE
                PS C:\> New-Choice -Choices 'Yes','No' -Caption "PowerShell makes choices easy"               
        .NOTES
                Author: Andy Schneider
                Date: 5/6/2011
#>

[CmdletBinding()]
param(
        [Parameter(Position=0, Mandatory=$True, ValueFromPipeline=$True)]
        $Choices,
        [Parameter(Position=1)]
        $Caption,
        [Parameter(Position=2)]
        $Message    
)

process {
        $resulthash += @{}
        for ($i = 0; $i -lt $choices.count; $i++)
            {
               $ChoiceDescriptions += @(New-Object System.Management.Automation.Host.ChoiceDescription ("&" + $choices[$i]))
               $resulthash.$i = $choices[$i]
            }
        $AllChoices = [System.Management.Automation.Host.ChoiceDescription[]]($ChoiceDescriptions)
        $result = $Host.UI.PromptForChoice($Caption,$Message, $AllChoices, 1)
        $resulthash.$result -replace "&", ""
        }        
}

#new-choice -Choices "yes","no","maybe"
new-choice -Choices "Yes","No","Copy ALL","Cancel (Abort Script)" -Caption "Continue the Copy Process?"

I want to make it so that if Yes, No or Copy ALL are selected, it takes the appropriate action, and if Cancel is selected it ends the script. However, I would also like for it to make the selecting of the Cancel button, hitting the ESC key, or closing out the window all do the same thing (end the script) as well.

On the above sample, it runs fine if one of those four are selected, and you click on the OK button (it returns the value of what was selected). However, if the Window is closed, the Cancel button is selected or the ESC key is typed, it generates the following error:

Exception calling “PromptForChoice” with “4” argument(s): “An error of
type “System.Management.Automation.Host.Promptin gException” has
occurred.” At C:\Documents and Settings\myPC\My Documents\test ui
prompt.ps1:34 char:43
+ $result = $Host.UI.PromptForChoice <<<< ($Caption,$Message, $AllChoices, 1)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

How can I trap those other “options” so that if one of them is selected, that it breaks out of the script & doesn’t throw the above error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:44:50+00:00Added an answer on June 17, 2026 at 4:44 pm

    Something like this should do what you want:

    try {
        $result = $Host.UI.PromptForChoice($Caption,$Message, $AllChoices, 1)
    } catch [Management.Automation.Host.PromptingException] {
        exit
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PowerShell script/function that works great when I use it in my
I have the following log entry that I am processing in PowerShell I'm trying
I have the following tiny PowerShell script that's meant to kill some certain processes
I have a small Powershell script that is used to shut down my virtual
I have the following PowerShell script that will parse some very large file for
I have the following code snippet from my PowerShell script that... Loops through a
I have the following Powershell code: Add-Type -Assembly System.Configuration [ExeConfigurationFileMap] $configMap = New-Object ExeConfigurationFileMap
I have following script that executes all the .reg files in the current directory.
My powershell script takes the following parameter: Param($BackedUpFilePath) The value that is getting passed
I have the following PowerShell code: function Get-SmoConnection { param ([string] $serverName = ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.