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:

  1. Get-Content "d:\projects\powershell\wakeup.dat"

 To save the content to a variable just use this:

  1. $data = Get-Content "d:\projects\powershell\wakeup.dat"

Each line of the file is an array element with the variable passed, so to display just the first line you could use this:

  1. $data = Get-Content "d:\projects\powershell\wakeup.dat"
  2. $data[0]

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:

  1. $data = Get-Content "d:\projects\powershell\wakeup.dat"
  2. write-host $data.count total lines read from file
  3. foreach ($line in $data)
  4. {
  5.     write-host $line
  6. }

Learn more: Q. Can PowerShell read and parse XML files?