Scripting Blog

A place to learn about PowerShell and share stories of automation

How Can I Pick Out and Save Specific Lines in a Text File?

Hey, Scripting Guy! I’d like to be able to read through a text file, select the lines that begin with a particular word (like Failure), and then save only those lines back to the same text file. Is there any way to do that?-- AC Hey, AC. Hey, AC. For simplicity’s sake, we’re assuming you have a text file that looks similar to this:...

How Can I Count the Number of Lines in a Text File?

Hey, Scripting Guy! How can I count the number of lines in a text file?-- MS Hey, MS. It’s easy to tell that Microsoft’s scripting technologies weren’t written by people who do system administration for a living. Our scripting technologies are jam-packed with features and capabilities, yet we always seem to missing the things that ...

How Can I Print Text Files Using a Script?

Hey, Scripting Guy! I have a bunch of text files in a folder. Is there a way to print those files using a script? I know I can use Microsoft Word to print the files from a script, but I don’t have Microsoft Word installed on that computer.-- MA Hey, MA. Don’t tell anyone from Microsoft we said this, but that’s OK; you don’t need ...

How Can I Save Output to a Text File?

Hey, Scripting Guy! Is there an easy way to save my script output to a text file rather than displaying it on screen? -- KP, Ogden, UT Hey, KP. If you always want your script to save data to a text file rather than display it on screen, then you’re probably better off using the FileSystemObject and its file-writing capabilities. However, it...

Can I Read a Text File From the Bottom Up?

Hey, Scripting Guy! I have a log file in which new data is appended to the bottom of the file; that means the most recent entries are at the end of the file. I’d like to be able to read the file starting with the last line and then ending with the first line, but I can’t figure out how to do that.-- MB, Milwaukee, WI Hey, MB. The...