Splitting Text Files using Powershell

Aug 16, 2013 powershell

A few days ago a friend asked why working with text files in powershell was so annoying. I asked him what the problem was, and he was trying to split a text file in half.

I gave him this simple line:

$content = (Get-Content C:\input-file.txt -Raw);
$content.Substring(0,$content.Length \* 0.5) | Set-Content "C:\first-half.txt"

This will get the raw text content of a file (C:\input-file.txt), and dump out the first 50% of the file to C:\first-half.txt