How to make a powershell choice menu call back to itself?

Brett Flagg 1 Reputation point
2023-11-29T17:10:20.47+00:00

With this script, after making a choice, how to make it loop back to the prompt again until I press "Q".
I've tried While ($true) {}, it does loop but the output is not visable..
This way I don't need the Write-Host output to remind me and 2 others how to run the tool...

param( [Parameter(Mandatory, Position = 1)] [string]$UserEmail )
cls
Write-Host "  " 
Write-Host "===============================================================" -Foreground Red
Write-Host "  First connect to Exchange, then after every choice press the " -Foreground Red
Write-Host "  the UP arrow to run the script again and you do NOT need to  " -Foreground Red
Write-Host "  reconnect to Exchange again as your session is still active. " -Foreground Red
Write-Host "===============================================================" -Foreground Red

$Title = "Email Archive Menu for $UserEmail" 
$Info = " " 
Write-Host "  "  

$options = [System.Management.Automation.Host.ChoiceDescription[]] @(
"&Connect to Exchange", 
"&Mailbox Size", 
"&Is Archiving Enabled?", 
"&Enable Archiving ", 
"Enable &Auto Expanding Archive ", 
"&Start Archiving Task ",
"&Quit")

[int]$defaultchoice = 6 $opt = $host.UI.PromptForChoice($Title , $Info , $Options, $defaultchoice) switch($opt)  
{
    0 {Invoke-Expression "Connect-ExchangeOnline" }      
    1 {Invoke-Expression "Get-Mailbox -Identity $UserEmail | Get-MailboxStatistics | Select-Object DisplayName, TotalItemSize"}     
    2 {Invoke-Expression "Get-Mailbox -Identity $UserEmail | Select ArchiveStatus, RetentionPolicy, AutoExpandingArchiveEnabled, ArchiveName"}     
    3 {Invoke-Expression "Enable-Mailbox -Identity $UserEmail -Archive" }     
    4 {Invoke-Expression "Enable-Mailbox -Identity $UserEmail -AutoExpandingArchive"}     
    5 {Invoke-Expression "Start-ManagedFolderAssistant -Identity $UserEmail"}
} 

Any Ideas anyone?

Thanks!

Windows for business Windows Server User experience PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-12-05T08:35:44.4333333+00:00

    Hello Brett,

    Regarding your post, I would like to clarify some more points:

    1. The expected loop should be happening to return to the wrtite-host part or the selection options?
    2. if you need to return to selection options, you could refer to the sample below.
    param( [Parameter(Mandatory, Position = 1)] [string]$UserEmail )
    cls
    Write-Host "  " 
    Write-Host "===============================================================" -Foreground Red
    Write-Host "  First connect to Exchange, then after every choice press the " -Foreground Red
    Write-Host "  the UP arrow to run the script again and you do NOT need to  " -Foreground Red
    Write-Host "  reconnect to Exchange again as your session is still active. " -Foreground Red
    Write-Host "===============================================================" -Foreground Red
    
    $Title = "Email Archive Menu for $UserEmail" 
    $Info = " " 
    Write-Host "  "  
    
    $options = [System.Management.Automation.Host.ChoiceDescription[]] @(
    "&Connect to Exchange", 
    "&Mailbox Size", 
    "&Is Archiving Enabled?", 
    "&Enable Archiving ", 
    "Enable &Auto Expanding Archive ", 
    "&Start Archiving Task ",
    "&Quit")
    $a=0
    while($opt -ne 6)
    {
    [int]$defaultchoice = 6 
    $opt = $host.UI.PromptForChoice($Title , $Info , $Options, $defaultchoice) 
    switch($opt)  
    {
        0 {Invoke-Expression "Connect-ExchangeOnline" }      
        1 {Invoke-Expression "Get-Mailbox -Identity $UserEmail | Get-MailboxStatistics | Select-Object DisplayName, TotalItemSize"}     
        2 {Invoke-Expression "Get-Mailbox -Identity $UserEmail | Select ArchiveStatus, RetentionPolicy, AutoExpandingArchiveEnabled, ArchiveName"}     
        3 {Invoke-Expression "Enable-Mailbox -Identity $UserEmail -Archive" }     
        4 {Invoke-Expression "Enable-Mailbox -Identity $UserEmail -AutoExpandingArchive"}     
        5 {Invoke-Expression "Start-ManagedFolderAssistant -Identity $UserEmail"}
    }
    }
    $opt=0
    
    1. if this is not what exactly you want, you are welcome to raise your idea in the post and I would check and reply.

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

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.