Reading Text Files

by Mar 9, 2009

Reading text files is easy using the Get-Content cmdlet:

$text = Get-Content $env:windirwindowsupdate.log

However, Get-Content reads the file line by line so you will most likely get back an array. This can cause problems when you really need the entire file content as text.

$text.GetType().FullName

If you need the complete text in one piece, try combining Get-Content with Out-String, which converts the array into one single string:

$text = Get-Content $env:windirwindowsupdate.log -Delimiter 'doesnotexist'
$text.GetType()
$text.length