Skip to content

Commit

Permalink
Add "foreach with Write-Progress" snippet (#4423)
Browse files Browse the repository at this point in the history
* WIP: Add Foreach with Status snippet

* Update total handling in foreach-progress snip

Co-authored-by: Justin Grote <JustinGrote@users.noreply.github.com>

* Update snippets/PowerShell.json

---------

Co-authored-by: Ian Morozoff <ian@ssfcu.org>
Co-authored-by: Justin Grote <JustinGrote@users.noreply.github.com>
Co-authored-by: Andy Jordan <2226434+andyleejordan@users.noreply.github.com>
  • Loading branch information
4 people authored Oct 7, 2024
1 parent 2384a06 commit 5f8a641
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions snippets/PowerShell.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5f8a641

Please sign in to comment.