locked
read-host compare string with text file RRS feed

  • Question

  • I have a text file like this

    123

    456

    789

    how do I compare my string to the text file and do a match? if it does not match prompt user to renter

    when I do 

    read-host "enter string"

    Wednesday, June 28, 2017 12:36 AM

All replies

  • What have you tried?

    You can use Select-String to find a string in a file:

    help select-string -full


    \_(ツ)_/

    Wednesday, June 28, 2017 1:01 AM
  • Hi Jon,

    here is an example:

    $text = Get-Content D:\test1.txt
    $prompt = Read-Host -Prompt 'number '
    if($prompt -in $text)
    {
        Write-Host 'welcome!' -ForegroundColor Green
    }else{
        Write-Host 'please try again...' -ForegroundColor Red
    }
    
    

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, June 28, 2017 3:03 AM
  • thank you

    I want to do a loop that keeps checking for a match until its in the txt file.

    how to do?

    Friday, June 30, 2017 3:30 PM
  • I want to do a loop that keeps checking for a match until its in the txt file.

    If that's really the requirement you could use an "until loop" and check in every cicle if the pattern matches.

    That could help you: PowerShell Looping: Understanding and Using Do…Until ... the rest you've already got from Andy.


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Friday, June 30, 2017 4:26 PM
    • Proposed as answer by Hello_2018 Friday, July 7, 2017 4:13 AM
    Friday, June 30, 2017 4:25 PM