r/PowerShell • • Feb 21 '20

Misc Powershell 7's parallel ForEach-Object is mind blowing.

I just installed v7 yesterday and have been putting it through the paces to see what I can use it for by overhauling some scripts that I've written in v5.1.

For my company's IAM campaign creation, I have a script that gets a list of all users in the company, then has to look up their manager. This normally takes roughly 13 minutes for ~600 users if I run it from my computer, 10 if I run it from a server in the data center.

I adapted the same script to take advantage of ForEach-Object -ThrottleLimit 5 -Parallel and it absolutely smokes the old method. Average run time over several tests was 1 minute 5 seconds.

Those that have upgraded, what are some other neat tricks exclusive to v7 that I can play with?

Edit: So apparently the parallel handles my horribly inefficient script better than a plain old foreach-object in 5.1 and optimizing the script would be better off in the long run.

198 Upvotes

71 comments sorted by

View all comments

Show parent comments

-1

u/Sunsparc Feb 21 '20

Something roughly like:

$users = get-aduser -filter * -properties manager,distinguishedname

foreach ($user in $users) {
$manager = get-aduser $manager -properties samaccountname
[pscustomobject] @{
Name = $user.name
Manager = $manager.samaccountname
}
}

13

u/Dogoodwork Feb 21 '20

$manager = get-aduser $manager -properties samaccountname

no...

3

u/nemec Feb 21 '20

Did your previous sequential script cache users in a hashtable? Since each user is (potentially) a manager of someone and managers usually manage more than one subordinate, looking up the manager in a hashtable before you try get-aduser $manager ... could massively speed up your code for the second, third, etc. employee who has $manager as a manager.