Khanh Le
Khanh Le

Reputation: 101

How to add a tab to a text file

I am running a for loop that would query for certain services and then output the services and their selected properties to a text file on each separate line where each property is separated by a tab, but I haven't figured out how to add a tab to a text file.

For instance, this is current set of strings in my code:

write-output $server, $servicename, $status, $startname | out-file -append output.txt

I would like to append the above string to output.txt, so it would appear like this:

server1 <tab> servername <tab> status <tab> startname
server2 <tab> servername <tab> status <tab> startname

Upvotes: 3

Views: 20761

Answers (4)

user2626301
user2626301

Reputation: 36

Even more flexibility is given working with arrays.

Let's say you've put your information in an array.

$servers[] = array($server, $servicename, $status, $startname);

Then you can implode the values with a tab, like this:

foreach($servers as $server)
  echo implode("\t", $server) . "\n";

Upvotes: -1

Robadob
Robadob

Reputation: 5349

Apparently you can use ``t` for tab in PowerShell. See Stack Overflow question Writing tabs to a file using PowerShell.

Most languages support \t though.

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

Try something like this:

"{0}`t{1}`t{2}`t{3}" -f $server, $servicename, $status, $startname |
    Out-File "C:\path\to\output.txt" -Append

If your input data is a list of objects, you may also be able to use Export-Csv or ConvertTo-Csv:

... | ConvertTo-Csv -Delimiter "`t" -NoTypeInformation |
    Out-File "C:\path\to\output.txt" -Append

Upvotes: 5

Adi Inbar
Adi Inbar

Reputation: 12323

"`t" will output a tab character.

Upvotes: 6

Related Questions