A: Reading the content of a file in PowerShell is very easy. You use the Get-Content cmdlet and the filename. Here’s an example:
To save the content to a variable just use this:
Each line of the file is an array element with the variable passed, so to display just the first line you could use this:
If you used $data.count, it would show the number of lines in the file. You can also access each line and perform some action by using the PowerShell foreach function, in this manner:
write-host $data.count total lines read from file
foreach ($line in $data)
Learn more: Q. Can PowerShell read and parse XML files?