Share via


Extra space in write-host output

Question

Friday, August 31, 2012 3:46 AM

I have function in a module which returns a paths -

function PoshPath([string] $tag)
{
    switch ($tag)
    {
        "sql"     { return $psscriptroot+"\scripts\sql" }
        "private" { return $psscriptroot+"\scripts\private" }
        "public"  { return $psscriptroot+"\scripts\public" }
        "sql"     { return $psscriptroot+"\scripts\sql" }
        default   { return $psscriptroot }
    }
}


I am facing a peculier issue where in the return string of the function is been appended with space,

I realized the issue is nothing related to a function as in the second call to write-host I can see the space inserted between Hello and \.ps1.

Why this extra space added and how can I get rid of this?

Thanks,

Shiju Samuel

All replies (2)

Friday, August 31, 2012 4:15 AM ✅Answered

As far as I've tested, this get's rid of the space...

write-host "$(poshpath -tag sql)\.ps1"

I guess having the whole expression enclosed in quotes forces powershell to treat it as one value.

Without the quotes, the expression is treated as two values, one in brackets, the other not... thus introducing the space.

Just a humble SysAdmin


Friday, August 31, 2012 4:17 AM ✅Answered

Write-Host "Hello"\*.ps1 -Separator ''

Сазонов Илья http://isazonov.wordpress.com/