Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
2.3k views
in PowerShell by 3 4 6

I requested to create and write data in a text file with append and show the written data in rows and column as a table using PowerShell, so How I can use PowerShell to write to a text file with append with a table format as below:

TaskID   |    TaskName 

1        |   write a text file on PowerShell
2        |   PowerShell write to text file append

PowerShell write to text file append with table

Thanks for any help!


1 Answer

1 like 0 dislike
by 161 202 397
selected by
 
Best answer

Write to a text file with append in a Table format using PowerShell

In PowerShell, you can use Add-Content to write data to a file.

Example

The below example adds a dummy data with append to a text file in a table format using Add-Content to write data, the n (to add a new line) and the t (to write a tab)

 # define the file path
 $filepath = "c:\debug.too.txt"

 #build Header
 Add-Content $filepath "TaskID`t| TaskName`t"

 #write data
 for($i=0;$i -lt 10;$i++)
 {
   Add-Content $filepath "`n$i`t| debug.to $i`t"
 }

 #open the file
 start $filepath

Output

Write to a text file with append in a Table format using PowerShell

Note: If you would like to write data in a table format using PowerShell, and there is no need to use a text file, it would be preferred to instead use a CSV file as mentioned in this example PowerShell access to the path is denied.

If you don’t ask, the answer is always NO!
...