Skip to content

Commit 5f8a641

Browse files
ian-morIan MorozoffJustinGroteandyleejordan
authored
Add "foreach with Write-Progress" snippet (#4423)
* WIP: Add Foreach with Status snippet * Update total handling in foreach-progress snip Co-authored-by: Justin Grote <[email protected]> * Update snippets/PowerShell.json --------- Co-authored-by: Ian Morozoff <[email protected]> Co-authored-by: Justin Grote <[email protected]> Co-authored-by: Andy Jordan <[email protected]>
1 parent 2384a06 commit 5f8a641

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

snippets/PowerShell.json

+19
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@
161161
"}"
162162
]
163163
},
164+
"Foreach with Progress": {
165+
"prefix": "foreach-progress",
166+
"description": "Insert a foreach loop with Write-Progress initialized",
167+
"body": [
168+
\\$progPercent = \"{0:n2}\" -f ([math]::round(\\$i/\\$array.count,4) * 100)",
169+
170+
Write-Progress -Activity \"${3:activityName}\" -Status \"\\$i of \\$array.count - \\$progPercent% Complete:\" -PercentComplete \\$progPercent",
171+
"\\$i = 1",
172+
"foreach ($${2:item} in $${1:array}) {",
173+
" \\$progPercent = \"{0:n2}\" -f ([math]::round(\\$i/\\$total,4) * 100)",
174+
" Write-Progress -Activity \"${3:activityName}\" -Status \"\\$i of \\$total - \\$progPercent% Complete:\" -PercentComplete \\$progPercent",
175+
" # Insert Code Here",
176+
" ${0}",
177+
" ",
178+
" \\$i++",
179+
"}",
180+
""
181+
]
182+
},
164183
"ForEach-Object -Parallel": {
165184
"prefix": "foreach-parallel",
166185
"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",

0 commit comments

Comments
 (0)