Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

Missing While or Until Keyword in Do Loop PowerShell

September 11, 2023
in Blog, Powershell
0
ADVERTISEMENT

Table of Contents

Missing While or Until Keyword in Do Loop PowerShell

We’re writing a PowerShell script to create a VirtualBox virtual machine. We want to print a menu to select Windows edition to install.

$menu = {
Write-Host "
Select an option:

1. Windows 10
2. Windows 11
"
Write-Host "Select an option and press Enter: "  -nonewline
} 
cls

$vmCreation = {VBoxManage createvm --name $vmName --ostype $ostype --register}
 
Do { 
    cls
    Invoke-Command $menu
    $select = Read-Host
    if ($select -eq 1) {$osName = 'Windows'; $osNumber ='10'; $ostype = 'Windows10_64'}
    if ($select -eq 2) {$osName = 'Windows'; $osNumber ='11'; $ostype = 'Windows11_64'}

    Switch ($select)
    {
        1 {Invoke-Command $vmCreation}
        2 {Invoke-Command $vmCreation}
    }
} 

While ($select -ne 6)

But we get the following error when running the command directly from a PowerShell console:

wrDayEE2FcT6Bb6g90QJbzM5fG1FsgVwVmshhZALA5Om6hxhxsNhEQLvttVB

That’s because when dealing with not the PowerShell Integrated Console. PowerShell just throws the text in the editor into the terminal window.

With the PowerShell Integrated Console (such as Windows PowerShell ISE), it did something differently (sending a message behind the scenes with the full script to make sure the full script gets run). We can do this because it’s Integrated.

That’s why you got the error when run at the command prompt, but when run in a script it should work fine.

uNBjeg87FYhnUmyGeZsakto8yfJvWvfelu3f07PChfOUqfZYCIiJxv3Bukss

To fix it, we need to change the code a little bit. PowerShell seems to consider the else block as separate statement, not attached to if. Modified the code to use the end of the if and else in the same line.

SvamGftqPQ6a4a8wstQ5Lb6WcVcXsZZ8ou887JWV3FY9tmoPwT0eweafmDTB

As you can see, now your script can be run from command prompt without any error.

Gxv9ftrfvcsessdasvt9fdczczxcsfdsdfwU6JME8I3dsddsSVgvycaWwFfWfGF65756WSr5345365fsd

And it should be run in script mode as well.

bdflI1IczoGURG9E1GApVwptDnkhul3dWJekrsBmXdDlfMmBZFZiWLwrmJv9

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Set Full Screen Resolution with No Guest Additions in VirtualBox

Next Post

How to Remove the Message Press any key to boot from CD or DVD

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory