Loop Creation with Powershell

Hello,

I am learning Powershell and along with that trying to work on different projects, I wanted to create type of loop with powershell command which check for existence of particular keyword “Hello” in “c:\temp\loop.txt” 9 times with the interval of 10 mints.

If it found that keyword “hello” after any interval of 10 mints then it must exit from the loop.

Can someone pls help on this?

What help exactly do you need? What have you tried so far?

Please keep in mind - This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

Hi,

I was using below command so that it should get exit from the loop if it found hello keyword in C:\temp\loop.txt else keep checking for the keyword hello after every 5 seconds.

Also i wanted to add 9 attempts.

$data = Get-Content -path C:\temp\loop.txt

do {
Start-Sleep -Seconds 5

} until ($data -contains “hello”)

You have to move the Get-Content command inside the loop. Otherwise you will check the initial state again and again and again. :wink:

… but actually … the comparison operator -contains checks for a single element in an array of elements. If your input text file has more words on the same line together with “hello” your condition will not work.

I’d recommend using

instead

So you have to add a counter and increase this counter with each loop iteration and check both conditions combined with an -or or an -and. :wink:

You may elaborate a little more detailed what you’re actually trying to achieve … there might be a better way than the one you think you should go. :wink:

Hi,

Thanks, I was able to manage and my script is now working as per my requirement.

Would you like to share your solution to help others having the same or a similar requirement in the future?

Thanks in advance.

I have a video showing a few diffferent loops for those that are new to the different types of Powershell Loops.

Learn 5 ways to do Powershell loops TODAY

Will you pick an old thread for every new video of you to promote it here??? :thinking:

This is not an advertising platform for your videos!!!

1 Like