header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
Create a script that loops through a variable of items and iterates through an array
Last Post 12 May 2012 02:49 PM by RiffyRiot. 1 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
BenjaminDiscoUser is Offline
New Member
New Member
Posts:1
Avatar

--
12 May 2012 11:06 AM
    Hey guys,

    I'm trying to create a script that loops through a list of names and updates 2 text files with information from the loop.
    It isnt working at all though.

    Here is my script:
     
    $TestFile1 = "C:\Powershell\TestFile1.txt"
    $TestFile2 = "C:\Powershell\TestFile2.txt"
    $i = 0
    $TestFiles = @() $TestFiles = @($TestFile1, $TestFile2)
    $names = ("adam", "kumiko", "dave", "liam")

    foreach ($name in $names)
    {
    $name + "Hello1" | out-file $TestFiles[$i]
    $name + "Hello2" | out-file $TestFiles[$i]
    $name + "Hello3" | out-file $TestFiles[$i]
    $i++
    }

    I really have a hard time understanding the logic behind having a loop and then inside the loop, iterating through an array.

    Any help would be really great.

    Thanks.

     

    RiffyRiotUser is Offline
    Basic Member
    Basic Member
    Posts:329
    Avatar

    --
    12 May 2012 02:49 PM
    This is as simple as it gets...

    $numbers=@('1','2','3') #array of numbers
    $letters=@('A','B','C') #array of letters

    foreach($number in $numbers){
    foreach($letter in $letters){
    write-host $letter $number
    }
    }

    Result:

    A 1
    B 1
    C 1
    A 2
    B 2
    C 2
    A 3
    B 3
    C 3

    Hope this helps!
    Rule #1: You should only use Format-Table as the final command in the pipeline and only when you're not using the output for anything else
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer