While

Before the while statement enters your code you must have a condition that needs to be True. The condition is tested at least 1 time before it could execute your codeblock inside the loop, that's a difference with the do-while statement.

Usage of while

while (condition) {
	# your code here
}	
		

Examples

Count to 20:

$i = 1

while ($i -le 20) {
	Write-Host $i
	$i++
}
		

A shorter notation. Don't forget the semi-colon (;) delimiter here!

$i = 1

while ($i -le 5) { Write-Host $i; $i++ }
		

An infinite loop. Use Ctrl+C to stop it.

$i = 1

while ("True") {
	Write-Host $i
	$i++
}
		

Another infinite loop:

$i = 1

while (1) {
	Write-Host $i
	$i++
}