# Powershell Array list

**URL:** https://forums.powershell.org/t/powershell-array-list/14268
**Category:** PowerShell Help
**Created:** [April 27, 2020, 10:21am UTC](https://forums.powershell.org/t/powershell-array-list/14268 "2020-04-27T10:21:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sharkminds](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@sharkminds](https://forums.powershell.org/u/sharkminds)
#### Post date: [April 27, 2020, 10:21am UTC](https://forums.powershell.org/t/powershell-array-list/14268/1 "2020-04-27T10:21:47Z")

</div>

I am looking for help in arrays and forloop for a powershell script. I have list of domains in first array I need to reiterate get-computer command and give searchbase from my second array.

The first index in domains list is the input for searchbase . We have 10 domains which I need to run the get-adcomputer one by one and at the same time I need to pass searchoubase for each domain against the OU from that particular domain.

Get-ADComputer -Server [First Array] -searchbase [Second array] -filter \* -Properties LastLogonDate,Name,Description,Created| Where-Object {$\_.LastLogonDate -lt $date.AddDays(-60)}| Select-Object Name,DistinguishedName,Created,LastLogonDate,Description,DNSHostName,Enabled

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 27, 2020, 10:44am UTC](https://forums.powershell.org/t/powershell-array-list/14268/2 "2020-04-27T10:44:33Z")

</div>

Madhav, welcome to [Powershell.org](http://Powershell.org). Please take a moment and read the very first post on top of the list of this forum: [Read Me Before Posting! You’ll be Glad You Did!](https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did).

When you post code or error messages or sample data or console output format it as code, please.  
In the “ **Text** ” view you can use the code tags “ **PRE** ”, in the “ **Visual** ” view you can use the format template “ **Preformatted** ”. You can go back edit your post and fix the formatting - you don’t have to create a new one.  
Thanks in advance.

What is your actual question? Please ask a clear specific question and provide additional info if feasible.

A general tip: You can use a nested loop when you want to iterate one list for each element of another list.

---

<div class="post-metadata">

### Author: ![sharkminds](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@sharkminds](https://forums.powershell.org/u/sharkminds)
#### Post date: [April 28, 2020, 3:34am UTC](https://forums.powershell.org/t/powershell-array-list/14268/3 "2020-04-28T03:34:22Z")

</div>

Let me explain my scenario and what I want to achieve… We have 2 forest and few child domains under second forest.

1st Forest

> **[Leading Distributor In Decorative Timber & Building Materials Leading...](https://www.forest.one/)**
>
> From timber to wall panelling ForestOne is Australia's leading independent distributor of timber and decorative surface materials. Request a sample today!

Second Forest

> **[ForesT2.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=forest2.com)**
>
> Choosing the right domain name can be overwhelming. Our personalized customer service helps you get a great domain.

> **[ForesT2.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=forest2.com)**
>
> Choosing the right domain name can be overwhelming. Our personalized customer service helps you get a great domain.

[b.forest3.com](http://b.forest3.com)

Now I want to find all the computers objects from computers OU from both the forest including child domain in csv output. Then move those objects for each parent and child domains in some stale OU in their respective domains OU.

To acheive this I tried to create array of all domains and child domains in array 1 and the computers OU for each child domains in second array. Then I was thinking of passing each array object to get-adcomputer in forloop but got stuck in how to pass object from second array to the same command…

Not sure if this makes any sense and is this the right way to do this… Any help would be appreciated…

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 28, 2020, 3:53am UTC](https://forums.powershell.org/t/powershell-array-list/14268/4 "2020-04-28T03:53:58Z")

</div>

> [@](#):
>
> To acheive this I tried to create array of all domains and child domains in array 1 and the computers OU for each child domains in second array.

That would only make sense if the OU structures of all targeted domains have the same structure. If not you should save the OU structure together with the according domains.

> [@](#):
>
> Then I was thinking of passing each array object to get-adcomputer in forloop but got stuck in how to pass object from second array to the same command….

For this kind of task you can use - I already mentioned it above - nested loops.  
Example:

```
$collection1 = ‘Col 1 Item 1’, ‘Col 1 Item 2’, ‘Col 1 Item 3’, ‘Col 1 Item 4’
$collection2 = ‘Col 2 Item 1’, ‘Col 2 Item 2’, ‘Col 2 Item 3’, ‘Col 2 Item 4’

foreach ($item1 in $collection1) {  
# do some preparations if needed with the $item1  
foreach ($item2 in $collection2) {  
#do something with $item2 together with $item1  
“Outer loop: {0} - inner loop: {1}” -f $item1,$item2  
}  
}
```

---

<div class="post-metadata">

### Author: ![sharkminds](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@sharkminds](https://forums.powershell.org/u/sharkminds)
#### Post date: [April 28, 2020, 5:14am UTC](https://forums.powershell.org/t/powershell-array-list/14268/5 "2020-04-28T05:14:39Z")

</div>

Thanks…

---

<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: [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/powershell-array-list/14268/6 "2024-05-16T20:29:48Z")

</div>


