Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##Prepend-Line.ps1
- ## Will take a string and prepend each line of a file and output to a different file.
- ## eg
- ## Given a file containing 3 lines called infile.txt:
- ## line 1
- ## line 2
- ## line 3
- ## using the command "Prepend-Line.ps1 -infile infile.txt -prepend asdf -outfile outfile.txt
- ## will create a file called outfile.txt containing:
- ## asdfline 1
- ## asdfline 2
- ## asdfline 3
- ## Usage:
- ## Prepend-Line.ps1 -infile %PATHTOFILETOREAD% -prepend %STRINGTOPREPEND% -outfile %PATHTOFILETOWRITE%
- ## -infile is the file to read
- ## -prepend is the string data to add to the front of each line
- ## -outfile is the file to write - if the file exists it will be overwritten
- ## eg:
- ## Prepend-Line.ps1 -infile c:\logs\monday.txt -prepend "Monday 18/01/2016" -outfile c:\logs\modmonday.txt
- param(
- [string]$infile=$(throw "-infile parameter is required"),
- [string]$prepend=$(throw "-prepend parameter is required"),
- [string]$outfile=$(throw "-outfile parameter is required")
- )
- $newfile=$null
- $wholefile=get-content $infile
- foreach($line in $wholefile)
- {
- $newline=$Prepend+$line
- $newfile=$newfile+$newline+"`n"
- }
- $newfile | out-file $outfile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement