I am trying to Write-Host
message and save it to a variable in shortest possible way.
Currently my code looks like so:
Write-Host "Branch with name $branch_name already exists!`nNew branch has not been created."
$message = "Branch with name $branch_name already exists!`nNew branch has not been created."
And of course it works. I made a special function to compress this:
function Write-Host-And-Save([string]$message)
{
Write-Host $message
return $message
}
$message = Write-Host-And-Save "Branch with name $branch_name already exists!`nNew branch has not been created."
However it didn't make any output on screen. What is more I think there must be a better solution than new function to do it. And I tried to find one. Unsuccessfully.
Write-Host "Branch with name $branch_name already exists!`nNew branch has not been created." >> $message
Write-Host "Branch with name $branch_name already exists!`nNew branch has not been created." > $message
Write-Host "Branch with name $branch_name already exists!`nNew branch has not been created." -OutVariable $message
Is there any way to short-circuit that script?