Power Shell Reading Text Files

Reading text files is pretty straightforward with Get-Content:

PS C:\> get-content boot.ini
[boot loader]
timeout=15
default=multi(0)disk(0)rdisk(0)partition(2)\WINDOWS
[operating systems]
multi(0)disk(0)rdisk(0)partition(2)\WINDOWS="Microsoft Windows XP
Professional"
/fastdetect /NoExecute=OptIn
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Windows Server 2003,
Enterprise" /
noexecute=optout /fastdetect
C:\CMDCONS\BOOTSECT.DAT="Microsoft Windows Recovery Console" /cmdcons
PS C:\

We can use -TotalCount to display a specified number of lines from a text file: PS C:\> get-content ADOXEXCEPTION.LOG -TotalCount 5 CADOXCatalog Error
Code = 80040e4d
Code meaning = IDispatch error #3149
Source = Microsoft OLE DB Provider for SQL Server
Description = Login failed for user 'scriptaccess'.
PS C:\> In this example, the first five lines of a log file are displayed. You can get the same result with this expression: PS C:\> get-content ADOXEXCEPTION.LOG |select-object -first 5 An advantage to this approach is that Select-Object also has a -Last parameter that you can use to display a specified number of lines from the end of the file.