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 8267307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:28:27+00:00 2026-06-08T05:28:27+00:00

How can I start a script over again? I have 3 switches and I

  • 0

How can I start a script over again? I have 3 switches and I want them to revert back to the beginning of the script.

Import-Module ActiveDirectory
Write-Host "--Please Login using a.account--"
#login
$credential = Get-Credential
#Main
Write-Host "--Remote Computer Rename v2.0--"
Write-Host "1. Query AD (Outputs to a text file)"
Write-Host "2. Quick computer rename"
Write-host "3. Quit"
$choice=Read-Host "Chose a number to continue"

#AD Query for computer
switch ($choice)
{
 1 {
Write-Host "--Enter first five characters of computer name or full computer name i.e.     USCLT--"
$cn=Read-Host 'Computer name'
$out="$cn*"
Get-ADComputer -Filter 'SamAccountName -like $out' >> c:\myscripts\dsquery.txt
Write-Host "Query complete.  See dsquery.txt saved to Desktop."
}

...rest of my code.

So after See dsquery.txt saved to Desktop.” I want it to go back to write-host portion.

  • 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-08T05:28:28+00:00Added an answer on June 8, 2026 at 5:28 am

    My personal favorite for checking user input is the do { } until () loop. Here is your code with the added loop, this will accomplish what your looking for:

    Import-Module ActiveDirectory
    Write-Host "--Please Login using a.account--"
    #login
    $credential = Get-Credential
    #Main
    do {
    Write-Host "--Remote Computer Rename v2.0--"
    Write-Host "1. Query AD (Outputs to a text file)"
    Write-Host "2. Quick computer rename"
    Write-host "3. Quit"
    $choice=Read-Host "Chose a number to continue"
    
    #AD Query for computer
    switch ($choice)
    {
     1 {
    Write-Host "--Enter first five characters of computer name or full computer name i.e.     USCLT--"
    $cn=Read-Host 'Computer name'
    $out="$cn*"
    Get-ADComputer -Filter 'SamAccountName -like $out' >> c:\myscripts\dsquery.txt
    Write-Host "Query complete.  See dsquery.txt saved to Desktop."
    }
    
    ...rest of my code.
    } until ($choice -eq 3)
    

    This is a pretty unique strategy in my opinion. I took this from Jerry Lee Ford’s book : Microsoft Windows PowerShell Programming for the absolute beginner

    you can read more about these and every other loop in powershell here : http://www.powershellpro.com/powershell-tutorial-introduction/logic-using-loops/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a wrapper which can execute a given script over SSH with
How can I show a message to console when start script using Start-Job? I
How I can start reading from a specific byte? I have following code: try
I have a class which represents exam,it can start/reset/pause exam, so the methods look
I have a loop in my script, which iterates over an array - myArray
I have a ruby code that triggers php script over https. Use case: The
Can you run some script before the uploading of a file starts in php?
I can start the service correctly if the service's Log on account has a
Well I can start the thread when I press togglebutton to ON and it
From where I can start For interaction with Facebook like post Image on wall,

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.