From bdd5923c4d39354893b7af6fea18e48211494101 Mon Sep 17 00:00:00 2001 From: ian-mor <78066011+ian-mor@users.noreply.github.com> Date: Mon, 7 Oct 2024 16:41:58 -0500 Subject: [PATCH] Add "`foreach` with `Write-Progress`" snippet (#4423) --------- Co-authored-by: Ian Morozoff Co-authored-by: Justin Grote Co-authored-by: Andy Jordan <2226434+andyleejordan@users.noreply.github.com> --- snippets/PowerShell.json | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/snippets/PowerShell.json b/snippets/PowerShell.json index a4bf015d90..bd478f1ff3 100644 --- a/snippets/PowerShell.json +++ b/snippets/PowerShell.json @@ -161,6 +161,25 @@ "}" ] }, + "Foreach with Progress": { + "prefix": "foreach-progress", + "description": "Insert a foreach loop with Write-Progress initialized", + "body": [ +\\$progPercent = \"{0:n2}\" -f ([math]::round(\\$i/\\$array.count,4) * 100)", + +Write-Progress -Activity \"${3:activityName}\" -Status \"\\$i of \\$array.count - \\$progPercent% Complete:\" -PercentComplete \\$progPercent", + "\\$i = 1", + "foreach ($${2:item} in $${1:array}) {", + " \\$progPercent = \"{0:n2}\" -f ([math]::round(\\$i/\\$total,4) * 100)", + " Write-Progress -Activity \"${3:activityName}\" -Status \"\\$i of \\$total - \\$progPercent% Complete:\" -PercentComplete \\$progPercent", + " # Insert Code Here", + " ${0}", + " ", + " \\$i++", + "}", + "" + ] + }, "ForEach-Object -Parallel": { "prefix": "foreach-parallel", "description": "[PS 7+] Process multiple objects in parallel using runspaces. This has some limitations compared to a regular ForEach-Object. More: Get-Help ForEach-Object",