Create HashTable With Multiple Values

Welcome Forums General PowerShell Q&A Create HashTable With Multiple Values

Viewing 8 reply threads
  • Author
    Posts
    • #222087
      Participant
      Topics: 6
      Replies: 10
      Points: 103
      Rank: Participant

      Hi,

      I think i need a hash table for this?
      I would like a script to loop through a hash table and for each line, pass the values to my function.

      I have a working solution for 2 columns or a key and a value i believe?:

      [/crayon]

      My issue is trying to add a second value, I would like the list to have 3 columns which i believe would be a key and 2 values?

      Example (which im sure is not how i should do it):

      [/crayon]

      Any help on achieving this would be much appreciated.

      Thanks

      Jamie

    • #222093
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      If I got it right you can simply use CSV data …

    • #222102
      Participant
      Topics: 6
      Replies: 10
      Points: 103
      Rank: Participant

      Hi Olaf,

      Thank you for your reply, i was considering a CSV format as had used that in the past for something but wasn’t sure that was the right thing with not actually reading from or outputting to a CSV.

      I have used your example script and it works but when i apply it to my script it doesn’t work as the vales are passed to the function as 0 and 1 rather than the variable value i.e. item1 and string_value1

      [/crayon]

      Sorry if i have missed something obvious.

      • This reply was modified 4 months, 3 weeks ago by JDobbsy1987.
    • #222111
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      Almost … 😉

      The -f (format) operator is for strings … but you don’t want strings – you want objects with properties in this case. 😉

      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7#format-operator–f

      https://ss64.com/ps/syntax-f-operator.html

    • #222117
      Participant
      Topics: 6
      Replies: 10
      Points: 103
      Rank: Participant

      Thank you so much, it is working now

      I played with those variables based on a previous script i had written but that uses $_.var which didn’t work, still getting used to powershell :).

      Thank you for your help

       

      Cheers

      Jamie

    • #222120
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      … but that uses $_.var which didn’t work, still getting used to powershell :).

      … difference between foreach and ForEach-Object … 😉

    • #222126
      Participant
      Topics: 6
      Replies: 10
      Points: 103
      Rank: Participant

      … but that uses $_.var which didn’t work, still getting used to powershell :).

      … difference between foreach and ForEach-Object … 😉

      Ah i kind of thought foreach was an alias of foreach-object (and to be fair i try not to use aliases in powershell but i keep seeing foreach) which i can now see they are not exactly the same, thank you for your help.

      Having read up on it a little now, i came across another post here which helps too (https://powershell.org/forums/topic/foreach-vs-foreach-object/), thank you.

       

      Cheers,

      Jamie

    • #222129
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      … Ah i kind of thought foreach was an alias of foreach-object

      Unfortunately it is and I think that wasn’t the best idea for an alias in this case. But it’s actually easy to memorize if it has a pipe in front of it then it is ForEach-Object. 😉

    • #222138
      Participant
      Topics: 6
      Replies: 10
      Points: 103
      Rank: Participant

      Cheers for the tip 🙂

Viewing 8 reply threads
  • The topic ‘Create HashTable With Multiple Values’ is closed to new replies.