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:
Get-Content "d:\projects\powershell\wakeup.dat"
To save the content to a variable just use this:
$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:
$data = Get-Content "d:\projects\powershell\wakeup.dat" $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:
$data = Get-Content "d:\projects\powershell\wakeup.dat" write-host $data.count total lines read from file foreach ($line in $data) { write-host $line }
Learn more: Q. Can PowerShell read and parse XML files?
0 comments
Hide comments