I'm trying to create an application that puts variables in a file (minedown.conf) using Tee-Object, but every time it goes to add something to the file it overwrites it. I'm using
$account = Read-Host "Enter your Account SID number"
"account = $account" | Tee-Object -FilePath c:\minedown\minedown.conf
$token = Read-Host "Enter your Authority Token"
"token = $token" | Tee-Object -FilePath c:\minedown\minedown.conf
$from = Read-Host "Enter your Twilio number"
"from - $from" | Tee-Object -FilePath c:\minedown\minedown.conf
I'm trying to make each of those a separate line.
As an aside, in PowerShell 3.0, the -Append switch was added to the Tee-Object
cmdlet.
Tee-Object
is not the CmdLet you are looking for, try Set-content
and Add-Content
.
$account = Read-Host "Enter your Account SID number"
"account = $account" | Set-content -Path c:\minedown\minedown.conf
$token = Read-Host "Enter your Authority Token"
"token = $token" | Add-Content -Path c:\minedown\minedown.conf
$from = Read-Host "Enter your Twilio number"
"from - $from" | Add-Content -Path c:\minedown\minedown.conf
The purpose of Tee-Object
is really to act as a 'T', in a pipe sequence, in order to send data from the input to output and to a file or a variable (in order to debug a pipe sequence for exemple).
As mentioned, Tee-Object (alias tee
) is for splitting output into two directions. On Linux (tee
) it is useful for going to screen & file. In PowerShell it is more for putting to screen and throwing it back on the pipeline as well as other stuff, but cannot do Append. Not really how you want it.
However, I needed to do the Linux way and have it show on the screen as well as write to a file (in append mode). So I used the below method to write it onto the pipeline first, then put it to the screen (with colors) and put it in a file that is being appended to rather than just overwritten. Maybe it will be useful to someone:
Write-Output "from - $from" | %{write-host $_ -ForegroundColor Blue; out-file -filepath c:\minedown\minedown.conf -inputobject $_ -append}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With