# Do While Loop

**URL:** <https://forums.powershell.org/t/do-while-loop/701>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/do-while-loop/701 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/do-while-loop/701/1 "2011-12-31T18:30:00Z")

</div>

by spoony123 at 2013-03-10 03:56:30

> Hi All - New member here. Just having a go with Do While Loops, when i use the below code i would have expected the word "test" to keep being created but it only does it once, as i understand the while criteria would always be true (until service stops of course) so the Do would keep happening?. Obviously missed something fundamental here - can you guys advise  
>   
>   
>   
> `do {write-host "test"}while (Get-Service | Where-Object {$.name -eq "Application Experience" -and $.status -eq "Started"})`

by spoony123 at 2013-03-10 04:12:32

> Think i have answered my own question using this code  
>   
> `do {write-host "Application Service Stopped"} until (Get-Service | Where-Object {$.Displayname -eq "Application Experience" -and $.status -eq "Runningâ€})write-host "started"`  
>   
> This does the job. Thanks

by MasterOfTheHat at 2013-03-11 14:18:21

> I assume you wanted to initiate a service start command and then display "Application Service Stopped" until the service was up and running? As you figured out, you were using $_.name instead of $_.DisplayName and "Started" instead of "Running".   
>   
> Btw, if you wanted to use a do… while loop instead of your do… until, you could have used the code below. The difference is the $_.Status conditional in the Where-Object command.  
> `do {	"Application Service Stopped"} while (Get-Service | Where-Object {($`_`.DisplayName -eq "Application Experience") -and ($.Status -ne "Running")})`  
>   
> _You could also tweak it a little bit to use filtering on the Get-Service command. Probably doesn’t make much difference in this case, follows the "filter early" practice.  
> `do {	"Application Service Stopped"} while (Get-Service -DisplayName "Application Experience" | Where-Object {$`_`.Status -ne "Running"})`

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:58pm UTC](https://forums.powershell.org/t/do-while-loop/701/2 "2024-05-16T20:58:13Z")

</div>

Closed on May 16, 2024, 8:58pm UTC


