Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add text to every line in text file using PowerShell

I'd like to add characters to the end of every line of text in a .txt document.

#Define Variables
$a = c:\foobar.txt
$b = get-content $a

#Define Functions
function append-text  
    {  
    foreach-Object  
        {  
        add "*"  
        }  
    }  

#Process Code
$b | append-text

Something like that. Essentially, load a given text file, add a "*" the the end of every single line of text in that text file, save and close.

like image 547
Joshua Avatar asked Feb 10 '11 01:02

Joshua


People also ask

How do I add text to a string in PowerShell?

In PowerShell, string concatenation is primarily achieved by using the “+” operator. There are also other ways like enclosing the strings inside double quotes, using a join operator, or using the -f operator. $str1="My name is vignesh."


1 Answers

No function necessary. This would do it:

$b|foreach {$_ +  "*"}
like image 136
Elroy Flynn Avatar answered Sep 22 '22 15:09

Elroy Flynn