There are plenty of ways how PowerShell can read in text files, and they can differ considerably in time. Check for yourself. The examples below illustrate different approaches and measure the execution times. Just make sure the path in the example exists, and if not, choose a large text file to play with.
# make sure this file exists, or else
# pick a different text file that is
# very large
$path = 'C:\Windows\Logs\DISM\dism.log'
# slow reading line-by-line
$text = Get-Content -Path $Path
# fast reading entire text as one large string
$text = Get-Content -Path $Path -Raw
# fast reading text as string array with one
# array element per line
$text = Get-Content -Path $Path -ReadCount 0
# reading entire text with .NET
# no advantage over -Raw
$text = [System.IO.File]::ReadAllText($path)
ReTweet this Tip!
Hi, contrary to what was stated in the post, I found that, consistently, option 1 was quicker than option 2, and 3 than 4.
Here's the times in milliseconds I got:
Option 1: 309.2098Option 2: 30.9511Option 3: 26.7828Option 4: 38.2147
Here is a more detailed explanation of this topic.
Why Get-Content Ain't Yer Friend'powershell.org/2013/10/21/why-get-content-aint-yer-friend'
Powered by IDERA