Questions tagged [foreach-object]
77 questions
9
votes
1 answer
Pass variables to Foreach-Object PowerShell 7
What I want:
I have a script, which copies CAB-Files to over a hundred Servers, and works with it on them.
This script runs for over a week.
As I learned about the Foreach-Object -parallel Feature, I thought this would speed up the script…

Sukram22
- 93
- 1
- 4
7
votes
1 answer
Write-Information does not appear to work in powershell foreach-object -parallel
I am new to powershell and just learning it. I have some experience in C#. I am trying to use the foreach-object -Parallel option but cant get all the Write-* functions to work.
function writeTest {
1..1 | ForEach-Object -Parallel {
…

tcochunk
- 83
- 1
- 6
5
votes
2 answers
Powershell: Why is my variable empty after using ForEach-Object -Parallel?
I am trying to gather data from several servers using ForEach-Object -Parallel. The variable I use is being populated within the loop, but when the loop finishes the variable is empty.
$DBDetails = "SELECT @@VERSION"
$VMs = ("vm1", "vm2", "vm3",…

Arthur Quenneville
- 65
- 4
5
votes
3 answers
Recursively call a function from itself inside a ForEach-Object -Parallel block - Function is not recognized inside the parallel block
First time asker here. Please be kind :)
I'm attempting to recursively get all directories in a parallel manner in hopes of decreasing the time it takes to traverse through a drive. Below is the code I've tried. Essentially what I'm looking to do…

Daniel
- 4,792
- 2
- 7
- 20
4
votes
4 answers
Powershell Foreach-Object -Parallel how to change the value of a variable outside the loop (track progress)
This code prints a simple progression, doing one thing at a time:
$files = 1..100
$i = 0
$files | Foreach-Object {
$progress = ("#" * $i)
Write-Host "`r$progress" -NoNewLine
$i ++
Start-Sleep -s 0.1
}
But if I want to do two things…

What am I
- 43
- 1
- 3
3
votes
2 answers
what is the scope of $_ in a foreach-object loop in Powershell
Here's a simplified version of the script I'm trying to write:
$i = 0
Get-ChildItem -Filter *.bat|
ForEach-Object {
Write-Host $_.Name
switch ($i) {
0 { Write-Host $_.Name}
1 { Write-Host $_.Name }
2 { Write-Host…

robynChill
- 45
- 3
3
votes
1 answer
Is there a way to get the TID (thread id) in powershell foreach-object -parallel?
Just wondering if I could get like a $TID for thread id, just like a $PID or process id, with foreach-object -parallel in powershell 7 or pwsh (https://github.com/PowerShell/PowerShell#-powershell). I can run this and see a bunch of threads with a…

js2010
- 23,033
- 6
- 64
- 66
3
votes
1 answer
Error handling within Foreach-object Parallel block - Powershell 7
What would be the best way to capture error within below Foreach-Object Parallel block as there would be three separate threads/runspaces running and executing code written in the block and multiple errors/exception can occurs at the same time?…

user13624867
- 225
- 4
- 14
3
votes
1 answer
Cannot start job with foreach-object in parallel
I have prepared this script to try to execute in parallel the same function multiple times with different parameters:
$myparams = "A", "B","C", "D"
$doPlan = {
Param([string] $myparam)
echo "print $myparam"
# MakeARestCall is a…

Manu
- 4,019
- 8
- 50
- 94
2
votes
2 answers
Foreach-Object -Parallel, How to implement a timeout at thread level
Foreach-Object -Parallel is great. But sometimes the script block will hang, which will block the rest of the code. There is a timeOutSeconds parameter on the Foreach-Object, but it's a global one. When the timeout is reached, any remaining object…

miljbee
- 300
- 3
- 8
2
votes
1 answer
How to handle -WhatIf inside ForEach-Object -Parallel script block
There is a script that has CmdletBinding attribute, which effectively makes it an “advanced” script. Inside the script I'm processing data in a pipeline in parallel, and I want the -WhatIf parameter to be passed down to the processing script block…

AKd
- 501
- 4
- 17
2
votes
1 answer
Error access to variable after try catch in PowerShell 7
i received this error on PowerShell 7:
Total Urls to process: 12496
i: 1 | total:
RuntimeException:
Line |
45 | $percent = [int](100 * $i / $lines)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Attempted to divide by…
user20780615
2
votes
0 answers
Powershell foreach-object different output - directly from convertfrom-json vs. through a temporary variable
My question is - why is the output generated by a) statement different to that by b) statement?
$json = '[{"name": "george",
"psw64": "AMnd8BFdd8BFd"
},
{"name": "lucy",
"psw64": "AQ8BFdERjHoAw"
}
]
'
# a) -- different output to b)…

Babla
- 53
- 5
2
votes
4 answers
Create file list in every subfolder using PowerShell
I'm very new to PowerShell and have basically zero knowledge about PowerShell scripting, which is why I need some help.
I have folder structure with subfolders (as shown below) for which I want to generate a filelist.txt in every 1st level subfolder…

redoXiD
- 23
- 1
- 4
2
votes
1 answer
PowerShell script to combine CSVs and add root directory as an additional column
I'm trying to write my first PowerShell script and I'm struggling to get my head around the object orientation and piping approach. So I've got a directory structure like this (Google Takeout Nest directories):
Path\DeviceID-A\
- Year\
-…

JamesM
- 71
- 1
- 6