<p>Peu de documentation sur les boucles PowerShell.</p>
<p>La documentation sur les boucles dans PowerShell est abondante, et vous pourriez consulter les rubriques d’aide suivantes : <a href="https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_for"><code>about_For</code></a>, <a href="https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_foreach"><code>about_ForEach</code></a>, <a href="https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_do"><code>about_Do</code></a>, <a href="https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_while"><code>about_While</code></a>.</p>
<pre><code class="lang-auto">foreach($line in Get-Content .\file.txt) {
if($line -match $regex){
Travail ici
}
}
</code></pre>
<p>Une autre solution idiomatique PowerShell a votre probleme est de diriger les lignes du fichier texte vers la <a href="https://learn.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Core/ForEach-Object">cmdlet <code>ForEach-Object</code></a> :</p>
<pre><code class="lang-auto">Get-Content .\file.txt | ForEach-Object {
if($_ -match $regex){
Travail ici
}
}
</code></pre>
<p>Au lieu de faire la correspondance regex a l’interieur de la boucle, vous pouvez diriger les lignes a travers <a href="https://learn.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Core/Where-Object"><code>Where-Object</code></a> pour filtrer uniquement celles qui vous interessent :</p>
<pre><code class="lang-auto">Get-Content .\file.txt | Where-Object {$_ -match $regex} | ForEach-Object {
Travail ici
}
</code></pre>