I'm running a PowerShell script against many servers, and it is logging output to a text file.
I'd like to capture the server the script is currently running on. So far I have:
$file = "\\server\share\file.txt"
$computername = $env:computername
$computername | Add-Content -Path $file
This last line adds question marks in the output file. Oops.
How do I output a variable to a text file in PowerShell?
After some trial and error, I found that
$computername = $env:computername
works to get a computer name, but sending $computername
to a file via Add-Content doesn't work.
I also tried $computername.Value
.
Instead, if I use
$computername = get-content env:computername
I can send it to a text file using
$computername | Out-File $file