How to repeat strings in output once each for each item in an array?

Welcome Forums General PowerShell Q&A How to repeat strings in output once each for each item in an array?

This topic contains 2 replies, has 2 voices, and was last updated by

1 month, 3 weeks ago.

  • Author
  • #114306

    Points: 24
    Rank: Member

    I've been struggling with this for a while and hope I can get ideas for a solution from here.

    I'd like my output to have repeating values in column1 and 2 for each item in a string array in column3.

    So something like ("a", "3", ("x","y","z")) would output as:
    a 3 x
    a 3 y
    a 3 z

    More specifically I'm working with Citrix and after quite a bit of experimentation I've come up with...

    Get-BrokerApplication | ?{$_.Enabled -eq $true -and $_.CommandLineArguments -like '\\server*'} | ForEach-Object { $($_.ApplicationName; $_.CommandLineArguments; $_ | Select-Object -ExpandProperty AssociatedUserFullNames) -Join ',' }

    ApplicationName and CommandLineArguments are string type.
    AssociatedUserFullNames is string[] type.

    The command meets one goal I had, all the values in the string array AssociatedUserFullNames can be read, before they were cut off inside {}.

    The output produced from the above is:
    App_x,\\server\sharex\users,Domain Admins
    App_a,\\server\sharex\XYZ,Domain Admins,XYZ Management,Company A
    App_b,\\server\sharey\Folder1,Domain Admins,Charlie Brown,Lucy van Pelt
    App_c,\\server\sharex\Folder2,Domain Admins,Charlie Brown,Lucy van Pelt
    App_d,\\server\sharex\users\%username%,Domain Admins,XYZ Management,Company A,Sopwith Camel,Fokker Tri Plane,Spad
    App_e,\\server\sharex\ProjMgt,Domain Admins,Engineer1,Engineer2,Engineer3,EngineerGroup1
    App_f,\\server\sharez\Reports,Domain Admins,Reports Users
    App_g,\\server\sharez\Reports-Special,Domain Admins,Staff1,Charlie Brown,Lucy van Pelt,Staff2,Staff3
    App_h,\\server\sharez\Requisitions PDFs,Domain Admins,Staff1

    It would be really helpful to get it into the final form described at the beginning of the post.


  • #114337

    Points: 259
    Helping Hand
    Rank: Contributor

    How about something like this:

    $array = ('a', '3', ('x','y','z'))
    foreach($element in $array[2]){
        "$($array[0]) $($array[1]) $element"

    Or if it's just a flat array instead of an array in an array you could use something like this:

    $array = ('App_b','\\server\sharey\Folder1','Domain Admins','Charlie Brown','Lucy van Pelt')
    foreach($element in $array[2..$($array.Count-1)]){
        "$($array[0]) $($array[1]) $element"
    • #114373

      Points: 24
      Rank: Member

      Thank you Olaf. You put me on the right track.

      My solution ended up being:

      $list = Get-BrokerApplication | ?{$_.Enabled -eq $true -and $_.CommandLineArguments -like '\\server*' }
      foreach ($item in 0..$($list.count-1)) { foreach ($name in 0..$($list[$item].AssociatedUserFullNames.count-1)) {"$($list[$item].ApplicationName) $($list[$item].CommandLineArguments)  $($list[$item].AssociatedUserFullNames[$name])" } }

You must be logged in to reply to this topic.