# Working with Multiple Arrays

**URL:** <https://forums.powershell.org/t/working-with-multiple-arrays/23397>\
**Category:** PowerShell Help\
**Created:** [January 11, 2024, 7:59pm UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397 "2024-01-11T19:59:16Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [January 11, 2024, 7:59pm UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397/1 "2024-01-11T19:59:16Z")

</div>

Hello,

I’m trying to figure out how I can build a new array using values from `$arrFullNames` if it contains the value in `$arrOldNames`.

```auto
$arrFullNames = @(Get-Content '\\mynas\ar_books\full_names.txt')
$arrOldNames = @(Get-Content '\\mynas\ar_books\old_names_5.txt')

```

**Note:**  
`$arrOldNames` contains only a list of old folder names like so: `old_name_1`  
`$arrFullNames` contains the full path and folder name like so: ‘`\\mynas\ar_books\old_name_1`’

For example, if ‘`old_name_1`’ is contained in `$arrFullNames`, then store that value from `$arrFullNames` into a new array called `$arrOldNames2`.

I can use the following to check if the `$oldName` exists in `$arrFullNames`…

```auto
foreach ($oldName in $arrOldNames) {
  if ($arrFullNames -like "*$oldName*") {
    Write-Host "$oldName exists"
  }
}

```

However, I can’t figure out how to retrieve the object in `$arrFullNames` to store it into a new array.

Any help would be greatly appreciated. Thanks…

---

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [January 11, 2024, 8:07pm UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397/2 "2024-01-11T20:07:17Z")

</div>

I think I just found something that will help me achieve my final goal with renaming the folders…  
I just need to test it out more to perform the actual renaming…

```auto
foreach ($oldName in 0..$arrOldNames.GetUpperBound(0)) {
  if ($arrFullNames -like "*$oldName*") {
    Write-Output "Full OLD name is $($arrFullNames[$line])"
    #Write-Output "OLD name is $($arrOldNames[$line])"
    Write-Output "NEW name is $($arrNewNames[$line])"
    #Write-Host "$oldName exists"
  }
}

```

I’ll be back… 😉

DISREGARD… that GetUpperBound(0) isn’t returning the correct NewName value I wanted… The `Write-Output "Full OLD name is $($arrFullNames[$line])"` is returning the first value in that array, not the one it’s being matched to.  
This is why I think I need the new array so that the new array will line up with the NewName array…  
If there’s a way to search and rename without a new array, that would be optimal…

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [January 11, 2024, 8:44pm UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397/3 "2024-01-11T20:44:31Z")

</div>

> [@zero269](#):
>
> ISREGARD… that GetUpperBound(0) isn’t returning the correct NewName value I wanted… The `Write-Output "Full OLD name is $($arrFullNames[$line])"` is returning the first value in that array, not the one it’s being matched to.  
> This is why I think I need the new array so that the new array will line up with the NewName array…  
> If there’s a way to search and rename without a new array, that would be optimal…

If I am understanding you properly, this might be what you’re looking for:

EDIT to code that actually works:

```powershell
$arrFullNames = @(Get-Content '\\mynas\ar_books\full_names.txt')
$arrOldNames = @(Get-Content '\\mynas\ar_books\old_names_5.txt')

$arr = $arrFullNames | Where-Object {
    $arrOldNames -contains ($_ | Split-Path -Leaf)
    }
}

```

That code just pulls the data from the text files in, loops through each `$arrfullnames` object, and if it is in $arroldnames, it puts it in the output… I’m just capturing all that in the $arr var, so assuming its more than one, it will be an array of strings. In any case, the hits should be in `$arr`. You can name it whatever you want of course, my brain couldn’t come up w/ a name haha. If I misunderstood though sorry 😃 I may just need further clarification on the ask.

---

<div class="post-metadata">

**Author:** ![masterofthehat](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/masterofthehat/32/5116_2.png) [@masterofthehat](https://forums.powershell.org/u/masterofthehat)\
**Post date:** [January 11, 2024, 8:50pm UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397/4 "2024-01-11T20:50:48Z")

</div>

I was trying SO hard to beat @dotnVo to the solution! lol!

This is the way I did it:

```auto
$arroldNames = @(Get-Content -path C:\temp\OldNames.txt)
$arrMatch = @()

$arroldNames | ForEach-Object {
    $name = $_
    $arrMatch += $arrFullNames | Where-Object {$_ -Like "*$name"}
}

$arrMatch

```

Both ways should give you same output. Mine could be simplified a little, but that just reflects the way my mind works. I didn’t think of looping through the array that already contained the data I needed to export, so I had to come up with a way for my comparison to return the matched value. Using -like in an if statement won’t do it, but that’s what Where-Object is made for.

Just shows the diversity of the language…

---

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [January 11, 2024, 9:29pm UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397/5 "2024-01-11T21:29:40Z")

</div>

Hi @dotnVo  
Thanks for your help with this.

I was playing around with your code, but $arr is coming up empty.  
I tried the following:

- Changed `-contains` to `-like`; still empty.
- Reversed arrays for both tests with `-contains` and `-like`; still empty.
- Used wildcards (\*) for both tests with `-contains` and `-like`; `$arrFullNames -like "*$_*"` returned the 5 values from the `$arrOldNames` instead of the matching values from the `$arrFullNames` array.

Just curious, but is the current PS Object (`$_`) the array for the one being piped to the `ForEach-Object` or the `IF` statement?

Ultimately I’m trying to return the matching value from `$arrFullNames` array based on the search value used from the `$arrOldNames` array.

Something along the lines of:  
if value in `$arrOldName` is found in `$arrFullNames`, then return the value found in `$arrFullNames`.

I’m using a wildcards because if the `$arrOldNames` value is: ‘`old name 1`’, the value in `$arrFullNames` will be slightly different and includes the full path to the folder; example:  
‘`\\mynas\ar_books\old name 1 - LG 3.1`’

I’m going to play around with @masterofthehat 's code in the time being…

Thanks again.

---

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [January 11, 2024, 9:37pm UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397/6 "2024-01-11T21:37:20Z")

</div>

I got a chance to test your code and the `$arrMatch` was populated with the correct matching value from the `$arrFullNames` array.

I gotta run, but when I get back I’ll update you on my progress with renaming the folders using the values in $arrNewNames.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [January 11, 2024, 9:38pm UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397/7 "2024-01-11T21:38:10Z")

</div>

Figured out why mine didn’t work for you: I didn’t see that the second var was a full path, my bad 🙂

EDIT: i did modify my code above so it should work now, just so its actually functioning, but it’s mostly just aligning with what Matt did

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [January 11, 2024, 9:45pm UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397/8 "2024-01-11T21:45:59Z")

</div>

Something like this should work:

```auto
$arrNew = $arrFullNames | Where-Object {
    $arrOldNames -contains ($_ | Split-Path -Leaf)
}

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [January 11, 2024, 10:16pm UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397/9 "2024-01-11T22:16:59Z")

</div>

Ha! yea this is what I should have done. `Split-Path` is an awesome little cmdlet.

---

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [January 12, 2024, 2:13am UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397/10 "2024-01-12T02:13:28Z")

</div>

**UPDATE** :

Hi _masterofthehat_,

Your solution works perfectly. I was able to test it again and use the new array it created to rename my 5 test folders.

I’ll likely use matt’s - more compact - code which produces the exact same results. I wish we could mark two solutions…? 🙂

My code so far with the help from you, @dotnVo & @matt-bloomfield

_Thanks to all of you and best regards…_

```auto
# Arrays
$arrFullNames = @(Get-Content '\\mynas\ar_books\full_names.txt')
$arrOldNames = @(Get-Content '\\mynas\ar_books\old_names_5.txt')
$arrNewNames = @(Get-Content '\\mynas\ar_books\new_names_5.txt')

# Match OldName to FullName (matt-bloomfield)
$arrNew = $arrFullNames | Where-Object {
   $arrOldNames -contains ($_ | Split-Path -Leaf)
}

# Rename FullName (arrNew) with NewName (folder)
for ($i = 0; $i -lt $arrNew.Count; $i++) {
   Rename-Item $arrNew[$i] $arrNewNames[$i]
}

```

---

<div class="post-metadata">

**Author:** ![masterofthehat](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/masterofthehat/32/5116_2.png) [@masterofthehat](https://forums.powershell.org/u/masterofthehat)\
**Post date:** [January 12, 2024, 3:01pm UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397/11 "2024-01-12T15:01:52Z")

</div>

Glad it works for you!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [January 12, 2024, 3:36pm UTC](https://forums.powershell.org/t/working-with-multiple-arrays/23397/12 "2024-01-12T15:36:00Z")

</div>


