Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Tee-Object without overwriting file

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.

like image 442
zoey cluff Avatar asked Mar 29 '13 03:03

zoey cluff


3 Answers

As an aside, in PowerShell 3.0, the -Append switch was added to the Tee-Object cmdlet.

like image 51
Shay Levy Avatar answered Nov 09 '22 11:11

Shay Levy


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).

like image 33
JPBlanc Avatar answered Nov 09 '22 10:11

JPBlanc


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}
like image 21
JoeB Avatar answered Nov 09 '22 11:11

JoeB