Powershell

[Powershell] – Get-Content – Read a Text File

Click to rate this post!
[Total: 0 Average: 0]

how to read a text file with powershell get-content


 How does the Powershell Get-Content cmdlet work? I present here, through some examples how to read a text file via Get-Content

step 1:
Create a file “myfile.txt” for example in c:\ temp\.
We will give a variable name to this file $ file the file contains 2 lines

line 1: hello
line 2: thank you

$file= “c:\temp\myfile.txt”

step 2:
The basic command to read / retrieve the content of the file is “get-content

In our example we will choose to store the content in a $ contents variable:

$contents = get-content $file

if we display the content of the file:
write-output $contents

the result will be:
Hello thank you 

 You now know how to read a file in powershell


Leave a Reply

Your email address will not be published. Required fields are marked *