4

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.

4 Answers 4

7

Simple, short, stupid:

& cmd /c pause
exit

This will even contribute the "Press any key" message the TO requested. If you prefer to stay in PowerShell:

Read-Host "Press any key to exit..."
exit

But we may also get the input back:

$reply = Read-Host "Please type EXIT to exit"
if ($reply -eq "EXIT") { exit; }

I like that Read-Host exits the script when typing Ctrl-C, like cmd's pause does.

1
  • 1
    But Read-Host "Press any key to exit..." allows the user to type anything before pressing enter to exit. So it really should be Read-Host "Press the enter key to exit...". That said, what is the command to exit when "any key" is pressed?
    – Clay
    Commented Jul 26, 2018 at 17:46
3

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
2

From a blog post I found:

echo "Press any key to exit..."
$Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown") | OUT-NULL
$Host.UI.RawUI.FlushInputbuffer()
1
  • 1
    Thank you very much, exactly what I was looking for
    – AymericB
    Commented Oct 11, 2019 at 13:15
0

Enclose the whole thing in a while (1) {} block. That creates an infinite loop that will only terminate if it encounters a break (end the loop) or exit (end the script). Presumably, option 3 will lead to one of those statements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.