locked
How to Use For loop or while loop in powershell RRS feed

  • Question

  • Hi All i am new to powershell and trying to work around with for loop or while loop. I am using the following script

        $FirstName = Read-Host "Please Enter First Name"

        IF([string]::IsNullOrEmpty($FirstName)) {            
        Write-Host "First Name cannot be left blank Please enter first Name"            
        } else {            
        Write-Host "Your Name is" $FirstName            
        }
    If the user does not enter any text i would like powershell to display message again for atleast 3 times to enter first name and than exits it. But dont know how to do it
    • Moved by Bill_Stewart Sunday, October 26, 2014 3:18 AM Help vampire question
    Friday, August 8, 2014 11:15 AM

All replies

  • This should do the trick:

    for ($x=0; $x -lt 3; $x++) {
    $FirstName = Read-Host "Please Enter First Name"

         IF([string]::IsNullOrEmpty($FirstName)) {           
         Write-Host "First Name cannot be left blank Please enter first Name"           
         } else {
         $x = 3          
         Write-Host "Your Name is" $FirstName           
         }

    }

    Friday, August 8, 2014 12:11 PM
  • Here's an example using a While loop:

    $PromptRetries = 0
    While ($PromptRetries -lt 3)
    {
    $FirstName = Read-Host "Please Enter First Name"
    
        IF([string]::IsNullOrEmpty($FirstName)) {            
        Write-Host "First Name cannot be left blank Please enter first Name"
        $PromptRetries++
        If ($PromptRetries -gt 3) { Exit }            
        } else {            
        Write-Host "Your Name is" $FirstName 
        Break           
        }
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, August 8, 2014 12:15 PM
  • There are good learning resources here: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    The Gallery also has very good script examples.

    The questions you ask are al basic. If you took the time to learn the fundamentals you would easily understand how to do most of these things.

    Here is a hint.

    help about_do

    This is the first thing you need to learn.  How to use help.  THe answers to most of your questions are available in help.

    Once you learn to use help and the learning resources yo will be on  your way to understanding how to use PowerShell.

    There are also lists of books.  Some are even free.

    Remember - you have been claiming to be "new to PowerShell" for a month now and are still asking the same questions.  Take some time to learn the basics.  PowerShell will be more fun.


    ¯\_(ツ)_/¯

    Friday, August 8, 2014 12:16 PM
  • One more example:

    $count = 0
    
    Do {
    
        $fn = Read-Host 'Enter first name'
    
        if ([string]::IsNullOrEmpty($fn.Trim())) {
    
            Write-Host "First name cannot be left blank. Enter first name."            
            
        } else {
                    
            Write-Host "Name is $fn"
            Break
    
        }
    
        $count++
    
    } Until ($count -gt 3)

    Make sure you look over the link jrv posted, those resources are really quite useful for getting the hang of things.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Friday, August 8, 2014 4:10 PM