Forever.Learner
Forever.Learner

Reputation: 37

Powershell, function add trailing space while concatenating string (parameters)

here's my issue :

function Dummy ($path1, $fileName1) {
    Write-Host "$path1$fileName1"
}

Dummy 'folder\', 'test2.exe'

result is :

folder\ test2.exe

I have just no idea why this whitespace is added. I tried different formatting option (trim, string formatting, even native .net one without result).

Regex kind of work as a "post issue" but is too extrem fo my usage (folder with whitespace in name)

Note that those option i tried work perfectly (or should i say "as expected" -_-') OUT of the function.

if anyone could tell me how dumb i am i'd be glad -_-'

NB : i keep adding a simple "hi" first line of this thread, it just get removed everytime so, sorry for this.

Upvotes: 1

Views: 340

Answers (2)

Ostemar
Ostemar

Reputation: 5808

It is because you are not sending in two parameters even though it might look that way. When you use a comma you are actually sending in an array. So $path will be an array with the values 'folder\' and 'test2.exe'. And of course $fileName1 will be empty.

So when you convert $path to a string the values will be separated by a space.

Remove the comma from the call and it will work:

Dummy 'folder\' 'test2.exe'

As a sidenote: I'm not quite sure what you are trying to do. But if you want to create paths I would recommend using:

Join-Path "folder" "test2.exe"

Upvotes: 2

wasif
wasif

Reputation: 15488

You have to do this:

function Dummy ($path1, $fileName1) {
    Write-Host "$($path1)$($fileName1)"
}

Dummy 'folder\', 'test2.exe'

Upvotes: 0

Related Questions