Waste space in output when using Write-Host

When I use Write-Host

within Foreach-Object

, I end up with unnecessary space in the output.

write-host "http://contoso.com/personal/"$_.ADUserName

      

Output:

http://contoso.com/personal/ john.doe

      

How can I remove the space before john? Trim does not work because there is no room in$_.ADUserName

+3


source to share


2 answers


This is because Write-Host treats your constant string and your object as two separate parameters - you are not actually concatenating to strings the way you call it. Instead of calling it that way, actually concatenate the strings:

write-host "http://contoso.com/personal/$($_.ADUserName)"

      

or



write-host ("http://contoso.com/personal/" + $_.ADUserName)

      

or



write-host ("http://contoso.com/personal/{0}" -f $_.ADUserName)

      

+6


source


Just do it without write-host

:



"http://contoso.com/personal/{0}" -f $_.ADUserName

      

0


source







All Articles