How to use IO.File AppendAllLines

Viewed 856
$text = "Hello","World"
[IO.File]::AppendAllLines(
    "C:\test.txt", 
    $text, 
    [Text.Encoding]::Default
)

And i get error:

'Cannot find an overload for "AppendAllLines" and the argument count: "3".'

How to fix it? WriteAllLines works great with same parameters, but what's wrong with this method?

1 Answers

Powershell has trouble with $text because the type is object[] but the method expects a string-enumerable.

Cast it to string[] first:


[string[]]$text = "Hello","World"

# or

[IO.File]::AppendAllLines(
    "C:\test.txt",
    [string[]]$text, 
    [Text.Encoding]::Default
)
Related