Powershell Array list

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

Madhav, welcome to 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!.

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.

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

1st Forest

forest1.com

Second Forest

Forest2.com

a.forest2.com

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…

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.

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
}
}

Thanks…