Reputation: 49
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?
Upvotes: 1
Views: 396
Reputation: 10754
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
Upvotes: 1
Reputation: 23355
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) -"
Upvotes: 3