This is a simple, but yet very useful task for anyone who scripts, how to read data from a text file. We will be utilizing the Get-Content cmdlet for this task.
In VBScript, we first had to create an object from the FileSystemObject. We then used the GetFile method to set another object to point to the data file itself. Next we would open the data file and then set up code to read the data file one line at a time and display each line as it was read. Then we would close the the file.
In Powershell, we can get a simple display of the contents of a data file simply by typing:
Get-Content DataFile.txt where Data File.txt is the path and name of the file that we want to read.
Lets say you want to count the number of lines. In VBScript, we would set a variable to increment with each line of test. In Powershell we type in the comment below:
Get-Content DataFile.txt | Measure-Object.
Count : 7
Average :
Sum :
Maximum :
Minimum :
Property :
The text file sampled above has 7 lines of text.
If you only wanted to return the count:
Get-Content DataFile.txt | Measure-object -property count
Count : 7
Comments