Parameters contained in a hash table

Welcome Forums General PowerShell Q&A Parameters contained in a hash table

Viewing 4 reply threads
  • Author
    Posts
    • #266327
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      I’ve been trying to get use a hash table to pass parameters in PowerShell as discussed here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7#splatting-with-hash-tables.

      Here’s a very simple example that shows the problems I’m having.

      When I run this (in 5.1 and 7), I get the following error message. I’ve tried this with other cmdlets as well and always gets the same result. Any idea what I’m doing wrong?

      Edit: I’m not sure why, but the image didn’t show up. You can see the output I get here: https://drive.google.com/file/d/1uDLTWb-sgc9UINesdgfxWP6Jq7k4a20e/view?usp=sharing.

      • This topic was modified 1 month ago by jbrusoe84.
    • #266339
      Participant
      Topics: 12
      Replies: 542
      Points: 1,319
      Helping Hand
      Rank: Community Hero
    • #266348
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      Thanks for the response, and I just added the spaces in like you have in your code. However, I get the exact same result. I also tried this on another computer and saw the same output.

      • This reply was modified 1 month ago by jbrusoe84.
    • #266354
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      Spaces aren’t the issue.  Look at line 6.  What using splatting (using a hash table to pass arguments), the variable is prefaced with an @ symbol not $.  Basically if you leave the $ in you are passing the object referenced by that variable as a positional argument (position 0) and not splatting across named parameters (in this example).

    • #266357
      Participant
      Topics: 1
      Replies: 2
      Points: 16
      Rank: Member

      Thank you. I guess I am so used to using $ that I totally overlooked that in the code and at the MS site.

Viewing 4 reply threads
  • You must be logged in to reply to this topic.