X

How to Read a File using PowerShell

If you are working as an admin on Windows Core Server and want to check the contents of a file, you can execute the following command:

get-content C:\testfile.txt

This command will display the contents of theĀ file using PowerShell. To save the contents in a variable, you can execute:

$FileContent = get-content C:\testfile.txt

To read the first few lines (I suppose the first five lines of the file here) you can use a for-each loop statement.

$filecontent = Get-Content info.txt
for( $i=0; $i -lt 5; $i++ )
 {
  Write-Host $filecontent[$i]
 }

The above script loops through the first 5 lines of the file that we loaded via Get-Content commands and prints them on the terminal.

Karim Buzdar: About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various websites.
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings