Unnecessary space in output when using Write-Host

前端 未结 2 644
南笙
南笙 2021-01-19 07:56

When I use Write-Host within a Foreach-Object, I get an unnecessary space in the output.

write-host \"http://contoso.com/personal/\         


        
相关标签:
2条回答
  • 2021-01-19 08:17

    Just do it without write-host:

    "http://contoso.com/personal/{0}" -f $_.ADUserName
    
    0 讨论(0)
  • 2021-01-19 08:26

    This is happening because Write-Host is considering your constant string and your object to be two separate parameters -- you aren't actually joining the strings together the way you're calling it. Instead of calling it this 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)
    
    0 讨论(0)
提交回复
热议问题