$a = ConvertFrom-Csv @'
Name,Age,
John,20
Amy,25
Joe,30
'@
$b = ConvertFrom-Csv @'
Name,Address
JohnDoe,123 street
AmyDoe,456 street
JoeSmith,789 drive
'@
Using this Join-Object script
/Join-Object Module
(see also: In Powershell, what's the best way to join two tables into one?), you might do something like this:
$a |Join $b -Using { $Right.Name -Like "$($Left.Name)*" }
Name Age Address
---- --- -------
{John, JohnDoe} 20 123 street
{Amy, AmyDoe} 25 456 street
{Joe, JoeSmith} 30 789 drive
To separate the names:
$a |Join $b -Using { $Right.Name -Like "$($Left.Name)*" } -Name A,B
AName BName Age Address
----- ----- --- -------
John JohnDoe 20 123 street
Amy AmyDoe 25 456 street
Joe JoeSmith 30 789 drive
Note that besides the fact that there is a risk that e.g. a Jon
will be matched with a JonathanDoe
, the -Using
parameter is rather slow.
If the full names are actually in camelCase
or PascalCase
, it will be faster to strip off the surname first (which might be done for both sides):
'JohnDoe' -CReplace '(?<=.+)[A-Z]+.*'
John
$a |Join $b -On { $_.Name -CReplace '(?<=.+)[A-Z]+.*' } -Name A,B
AName BName Age Address
----- ----- --- -------
John JohnDoe 20 123 street
Amy AmyDoe 25 456 street
Joe JoeSmith 30 789 drive