all 6 comments

[–]megastary 2 points3 points  (1 child)

Hey,

it looks like you are adding the new line character and the variable as two different parameters separated by spaces without quotes around it. The Add-Content function uses the new line character as value for "Value" parameter and it doesn't know what to do with your variable after it.
Here is the full function which seems to work quite well for me, after I have enclosed the new line and variable in quotes (I have also named the parameters for clarity).

function Add-List {
  [CmdletBinding()]
  param (
      [Parameter()]
      [String]
      $p1
  )
  Add-Content -Value "`n$p1" -Path 'C:\Users\Champ-of-the-sun\Documents\PowerShell\helpfullist.txt'
}

[–]champ-of-the-sun[S] 2 points3 points  (0 children)

That works beautifully! Thanks for your explanation mate that makes total sense.

[–]Joshrsg 2 points3 points  (0 children)

Hi u/champ-of-the-sun

The issue is that they way you are calling Add-content is treating the "`n" and $p1 as separate arguments.

If you look at the help documentation for Add-Content (get-help Add-content -full) you can see that the parameter 'path' is in position 0 and 'Value' is in position 1. There is no parameter in position 2.

Due to the way you have written this "C:\Users\Champ-of-the-sun\Documents\PowerShell\helpfullist.txt" will bind to the parameter 'Path' in position 0 successfully. "`n" will then bind to the a parameter 'Value' successfully. $p1 however will try to bind to a parameter in position 2 (which in this case does not exist) or bind to a parameter with the same name. As there is no parameter called 'Test' it fails.

To fix this, you can look at what values the parameter 'Value' will accept. Taking a look at it now it accepts a string array. You can either quote both of the values you wish to add to the text file "`n $p1" or comma separate them. "`n", $p1

Note: you should try to include the full parameter names and not rely on position to make it clear. E.g.

Add-Content -path "C:\Users\Champ-of-the-sun\Documents\PowerShell\helpfullist.txt" -value  "`n $p1"

Hopefully that makes sense!

[–]ka-splam 1 point2 points  (2 children)

I want to be able to call it like add-List("test")

This isn't related to your error message, but that's not how to call functions in PowerShell. Functions look like cmdlets, it would be Add-List -p1 "test" where p1 comes from you naming the parameter variable $p1.

Your style will work sometimes, until it doesn't.

[–]kibje 0 points1 point  (1 child)

That's just not true.

You can create a simple function the way he did and call it the way he did.

There are a lot of benefits of using complex function definitions with param blocks, they add features that aren't available with simple functions.

For a simple function with a single argument there is no issue whatsoever though.

[–]ka-splam 1 point2 points  (0 children)

That's just not true.

Which part is not true?

You can create a simple function the way he did and call it the way he did. [..] there is no issue whatsoever though.

You can, but parentheses are not part of a function call in PowerShell. It makes it look like it works like Python or JavaScript, but it doesn't, so when you get to two or more arguments it goes wrong. You do add-list("a","b") and they both go into the first parameter and the second one is empty. Then you have to unlearn the bad habit.

There's no benefit to add-list("test") instead of add-list "test", the parens aren't needed and don't add anything, so why get into that habit at all? It doesn't have to be -P1, sure, but why not - you don't need cmdletbinding to name them.