View Flat File Contents Using PowerShell

Bradley Schacht is a Principal Program Manager on the Microsoft Fabric product team based in Saint Augustine, FL. Bradley is a former consultant, trainer, and has coauthored 6 SQL Server and Power BI books, most recently Learn Microsoft Fabric. As a member of the Microsoft Fabric product team, Bradley works directly with customers to solve some of their most complex data problems and helps shape the future of Microsoft Fabric. Bradley gives back to the community through speaking at events such as the SQLBits, Fabric Community Conference, PASS Community Data Summit, SQL Saturdays, Code Camps, and user groups across the country including locally at the Jacksonville SQL Server User Group (JSSUG). He is a contributor on SQLServerCentral.com and blogs on his personal site, BradleySchacht.com.




The results will show the entire file scrolling by on the screen until the end is reached:
Now for the perfect use case; I have a file on my computer that is just over a gig in size; the export of the Contoso Fact Online Sales table containing about 12 million records. This file will never open in Notepad. It also isn't useful to have the entire file stream through a PowerShell window (it's probably not useful on the previous file either honestly). In most cases we just need to take a quick look at the data to see what we are dealing with. The Get-Content cmdlet can be extended to bring in only a subset of the data. This time at the prompt type Get-Content C:FactOnlineSales.txt -totalcount 5
This will bring back the first 5 records from the file. A perfect way to preview the file without needing to open something that is potentially gigs in size. Check out below for some cool functions and samples using the ProductModel file we just created above. Bring back the first x rows of the file: Get-Content FilePath -TotalCount X Example: Get-Content C:ProductModel.txt -TotalCount 5 Bring back the last x rows of the file: Get-Content FilePath |Select-Object -Last X Example: Get-Content C:ProductModel.txt | Select-Object -Last 5 Some file statistics: Get-Content FilePath | Measure-Object Example: Get-Content C:ProductModel.txt | Measure-Object 