PowerShell How to loop a script based on script array count

Viewed 337

I'm new to PowerShell. I'm trying to pull a users name and place it in a file. I have two corresponding arrays, $title and $csvfile. $title[0] corresponds with $csvfile[0] and $title[1] to $csvfile[1] and so on. Is it possible to loop this script while increasing the index for both at the same time so that each index runs once but also in sync?

$title = @('jim' 'john' 'james')
$csvfile = @('jim.csv' 'john.csv' 'james.csv')

Get -ADUser -filter {(Title -like "$title") -and (Company -like "Location1")} | Export-Csv c:\temp\$csvfile

Problem 2 The $title array doesn't seem to be iterating one at a time. If I replace $title[$] with any of the listed array items it works. Funny thing is that it DOES create all my .csv files from the $csvfile array, they are just empty. I've done some looking on the web, not sure if my array items are to long or the quotations are not parsing right. Any help would be muchly appreciated.

    $title = @(
   'Director of Nursing'
   'Assistant Director of Nursing'
   'Activities Director'
)
   $csvfile = @(
  'DON'
  'ADON'
  'ACTIVITIES'
)

    for($i=0; $i -lt $title.Count; $i++)
{
    #Get-ADUser -Filter { (Title -like "Director of Nursing")  -and  (Company -like "location1") }| Export-Csv c:\temp\$($csvfile[$i]).csv"
    Get-ADUser -filter { (Title -like "$($title[$i])")  -and  (Company -like "Location1") }| Export-Csv "c:\tempPath\$($csvfile[$i]).csv"
}
1 Answers

If I'm understanding this correctly. You'd like to append the user information from Get-Aduser to the corresponding csv of the users name you got it from? Such as, jim info goes to jim.csv, and so on for each one?

Seems like you're looking for the Foreach loop.

$title = @('jim','john','james')
#$csvfile = @('jim.csv' 'john.csv' 'james.csv')

Foreach($user in $title){
Get-ADUser -filter {(Title -like "$title") -and (Company -like "Location1")} | Export-Csv "c:\temp\$title.csv"}

A Foreach loop goes through a list of objects and performs the same action for every object, ending when it's finished with the last one. The list of objects is typically an array. When you run a loop over a list of objects, we say you're iterating over the list.

The Foreach loop can be used in 3 different ways: Foreach Statement, Foreach-Object cmdlet, or as a foreach() method.

What we're using here is the Foreach statement which is followed by parentheses that contain three elements, in order: a variable, the keyword in, and the object or array to iterate over. As it moves through list ($title-array in this case), Powershell will copy the object it's looking at into the Variable defining each item in the list, $user.

Note: because the variables is just a copy, you cannot directly change the item in the original list.

Please note as well, the items in an array are read separately by adding a comma to the end of each item in the list(if its not in a new line). In the code above, we're appending the same name you're iterating with to the csv file as well.

EDIT: Using for loop. . .


$title = @('jim','john','james')
$csvfile = @('CEO','CFO','CIO')


For($i=0; $i -lt $title.Count; $i++){
Get-ADUser -filter {(Title -like "$($title[$i])") -and (Company -like "Location1")} | Export-Csv "c:\temp\$($csvfile[$i]).csv"}

Matches the output like so:

jim - CEO.csv
john - CFO.csv
james - CIO.csv
Related