The result of your code isn't (or at least, may not be) a union of two arrays. A union should be a list of all the unique items in the arrays. Your code is simply adding the contents of one array to another array. In other words, if you had two lists: 1,3,5,6 and 1,2,3,4,7, the union would be 1,2,3,4,5,6,7 -- not 1,2,3,3,4,5,6,7.
Here's the way I would do this. I offered two alternative ways of producing the union. Either way works.
[array]$OuterActivityNames = @()
if ($PipelineDataset.Activities.Name.GetType().Name -eq 'String'){
$OuterActivityNames=$PipelineDataset.Activities.Name.Split(",")
}
else{
$OuterActivityNames=$PipelineDataset.Activities.Name
}
[array]$ActivitiesName= ( [regex]::Matches( (Get-Content $TempFilePath), $pattern).groups | Where-Object Name -EQ '1' ).Value
# To get the union of the two arrays you can do this
# to produce an unsorted list
$union = @{}
$u = ${}
ForEach ($o in $OuterActivityNames){
$union[$o] = 1
}
ForEach ($a in $ActivitiesName){
$union[$a] = 1
}
$union.keys
# Or, if you prefer, produce a sorted list
$u = @()
$u = ($OuterActivityNames + $ActivitiesName) | Sort-Object -Unique