PowerShell provides the Get-Content command or cmdlet in or to print the specified file, variable, or object data into the console. By using the Get-Content command a text file can be read and printed into the console or a variable value can be printed into the console too.
Print File Content To Console
The Get-Content command can be used to print a file content to the console. The file name or path can be provided directly to the command.
PS> Get-Content Names.txt
The output is like below.
Ismail Baydan Ahmet Ali Baydan Elif Ecrin Baydan
Alternatively we can specify the full path of the file to print the contents of the file.
PS> Get-Content D:\Person\Names.txt
The Get-Content Command provides the -Path attribute which can be used to specify the path of the given file. This is a more formal and reliable way.
PS> Get-Content -Path D:\Person\Names.txt
Print Specified Number of Lines To Console
The Get-Content command provides the -TotalCount attribute which is used to specify the number of lines that will be printed to the console. The lines start from the beginning of the file. In the following example, we will set 1 the -TotalCount, and only the first line will be printed to the console.
PS> Get-Content -TotalCount 1 Names.txt
The output is like below.
Ismail Baydan
Print Specified Lines of File To Console
One of the most powerful features is that printing the specified lines. The lines are specified with the -TotalCount attribute and index operators. In the following example, we will print the line numbers 5 and 10. So first we will return the first 10 lines and then use [-5] in order to return the last 5 lines which are between 5 and 10.
PS> (Get-Content -TotalCount 1 Names.txt)[-5]
Print Last Line of File To Console
The Get-Content command supports the -Tail attribute which is used to print a specified number of lines from the end of the file. If 1 is specified as a parameter to the -Tail attributes the last line of the given file is printed.
PS> Get-Content -Tail 1 Names.txt
Alternatively the -Tail attribute can be used to sprint specified number of lines from the end of the file. In the following example we will print the last 5 lines.
PS> Get-Content -Tail 5 Names.txt
Print Multiple Files Content
The Get-Content command can be used to print multiple files content. We will just provide the path you want to print contents to the console or specified location. The asterisk or * provided to express that iterate over all files and print their content.
PS> Get-Content "C:\Users\ismail\Desktop\*"
Alternatively the -Path attribute can be provided to make command more reliable.
PS> Get-Content -Path "C:\Users\ismail\Desktop\*"
Filter Names For Get-Content To Print
Even we can print multiple files content we can also filter the files according to their names or extensions by using the -Filter attribute. The asterisk or * used to match anything and we can specify the mathcing part. In the following example we will print all text files or *.txt extension files conten.
PS> Get-Content -Path "C:\Users\ismail\Desktop\" -Filter *.txt
Another example where we will print the log files content which have the *.log extension like below.
PS> Get-Content -Path "C:\Users\ismail\Desktop\" -Filter *.log
Return Single Line String
The complete single or multiple file content can be returned as a single string content by using the Out-String command. The Get-Content command output will be redirected into the Out-String command.
PS> $line = Get-Content -Path Names.txt | Out-String
Also multiple files content can be returned as a single string like below. In the following example all *.txt files in the current working directory will be put into the string named $line.
PS> $line = Get-Content -Path *.txt | Out-String
Get File Content As Byte Array
The Get-Content can read a specified file or stream and then convert it into the byte array which can be set into a variable which type will be a byte array. The -AsByteStream and -Raw attributes are used to read content as a byte array.
PS> $byte_array = Get-Content -Path Names.txt -AsByteStream -Raw