Efficient Lookups in PowerShell - Nero Blanco IT Migration Specialists
-
Twan van Beers
-
21 November, 2019
When writing scripts in PowerShell we often need to do a lookup in another dataset. Of course one way is to use an external tool like Excel with VLOOKUP or SQL Server with its joins and produce a data set that can be used in PowerShell without any lookups, but sometimes this just isn’t practical.
I often have a dataset in csv format, and in this example assume I have a two column csv; GroupSMTPAddress and MemberSMTPAddress.
After importing the dataset into a variable
$GroupMembers = Import-CSV .\groupmembers.csv
Then I can do things like
$Member = $GroupsMembers | ? { $_.MemberSMTPAddress -eq 'me@here.com' }
All well and good and it returns me an array of matched rows. The problem arises when you have tens of thousands of rows in this csv or large numbers of columns, since reading through that even in memory isn’t that fast. (Especially if you need to do that lookup thousands of times)
So a better way…
$GroupMembers = Import-CSV .\groupmembers.csv | group -AsHashTable -Property MemberSMTPAddress
So what this is doing is creating a hash table with the property as the key and the row as a hashtable.
Now when you want all the groups that me@here.com is a member of you can simply do
$GroupMembers.'me@here.com'
and you get the same array as before, but in a faction of the time!!! The me@here.com could be in a variable too
$MemberEmail = 'me@here.com'
$GroupMembers.$MemberEmail
And there you have it. Efficient lookups in PowerShell!
Twan van Beers
Twan is a senior consultant with over 30 years of experience. He has a wide range of skills including Messaging, Active Directory, SQL, Networking and Firewalls. Twan loves to write scripts and get deep and dirty into debugging code, in order to understand and resolve the most complex of problems.
News & Insights
Trusted insights on technology and innovation to power your business growth.
-
03/06/2026
A practical engineering guide to owners, assignment, consent, credentials and ...
Read More
-
19/05/2026
Domain migrations in Microsoft 365 are rarely as simple as shifting email ...
Read More
-
12/05/2026
Over the past 10 to 15 years, Microsoft 365 has fundamentally reshaped how ...
Read More
-
07/05/2026
Yesterday we had an all company meeting and I thought it would be cool to kick ...
Read More
-
07/05/2026
Planning a tenant-to-tenant migration? Talk to us
Read More
-
31/03/2026
In tenant-to-tenant (T2T) and Google-to-Microsoft 365 migration projects, ...
Read More
-
27/03/2026
You’d think this would be easy and not an uncommon request. There are genuine ...
Read More
-
23/03/2026
Managing Google to Microsoft Migration with GAM7 and PowerShell
Read More
-
16/03/2026
When a user leaves an organisation in Microsoft 365, administrators have ...
Read MoreSubscribe to our newsletter for the latest updates and insights.
Like what you see? Stay in touch! Subscribers to our email list are among the first to receive the latest news, views and updates from Nero Blanco
Efficient Lookups in PowerShell - Nero Blanco IT Migration Specialists">