0

Can someone explain why there are different outputs here? I'm trying to simply concat a path with a filename. For some reason, this works fine on the cmd line, but things are out of order in the FN.

function testConcat ($path, $fileName) {

    $testConat2 = "$path\$fileName.txt"
    write-host $testConat2

}

cd c:\temp

$pathParam = get-location
$fileNameParam = "test"
$testConat1 = "$pathParam\$fileNameParam.txt"
write-host $testConat1 #outputs C:\temp\test.txt
testConcat ($pathParam, $fileNameParam) #outputs C:\temp test\.txt
jbd
  • 413
  • 5
  • 14
  • 4
    **In PowerShell, functions are invoked _like console (terminal) programs_** - `foo arg1 arg2` - _not_ like C# _methods_ - `foo(arg1, arg2)` - see [`Get-Help about_Parsing`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing). If you accidentally use `,` to separate your arguments, you'll construct an _array_ that a function sees as a _single_ argument. To help avoid accidental use of method syntax, you can use [`Set-StrictMode -Version 2`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode) or higher. – mklement0 Sep 26 '18 at 22:26
  • 2
    @mklement0 is spot on. Try `testConcat $pathParam $fileNameParam` – lit Sep 26 '18 at 22:37

0 Answers0