PowerShell - Read file using Get-Content

by
Jeremy Canfield |
Updated: April 26 2023
| PowerShell articles
Get-ChildItem can be used to list the files and directories in a directory.
> Get-ChildItem -path C:\Users\john.doe\Documents
Directory: C:\Users\john.doe
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 8/1/2022 11:37 PM 11 example.txt
Get-Content can then be used to read the content of a file. In this example, the example.txt file contains the line "Hello World".
> Get-Content -path C:\Users\c065234\Documents\example.txt
Hello World
Of course, most files have many lines of content. Select-String can be used to only return the lines in the file that match a pattern.
Get-Content -path C:\Users\c065234\Documents\example.txt | Select-String foo
Did you find this article helpful?
If so, consider buying me a coffee over at