简体   繁体   中英

PowerShell unable to remove space between text and String

In PowerShell I am trying to find a way to remove the text from an output of text and a String.

Write-Host 'File located at C:\'$Fileline.FilePath -

I get an output of

c:\ program files\path

The space between c:\\ and "Program files" is what I want to remove. Do I have to convert the text to a string, and then output it as two strings and then remove the spaces?

This is happening because you are passing multiple strings to Write-Host , which it is then joining with spaces. This behaviour is somewhat unique to Write-Host .

You can meet your need by sending a single double quoted string to Write-Host , which you can then put your variable inside and it will be expanded. However because you are accessing a property of your variable, you need to wrap it in a sub-expression: $() :

Write-Host "file located at C:\$($Fileline.FilePath) -"

Try using the PowerShell -f formatting operator:

Write-Host ("File located at C:\{0} -" -f $FileLine.FilePath)

There's good info on -f at SS64 and at TechNet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM