EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Get-Content
Secondary Sidebar
PowerShell Tutorial
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell Get-Content

By Chirag NagarekarChirag Nagarekar

PowerShell Get-Content

Introduction to PowerShell Get-Content

Get-Content in the PowerShell is used to read the content from the file (text files) or the program from the specified location. This cmdlet reads the content of the file one at a time and returns as a collection of objects. From PowerShell 3.0 onwards, you can get the specified number of lines from the beginning or the end of the item.

Syntax:

Get-Content
[-ReadCount <Int64>] [-TotalCount <Int64>] [-Tail <Int32>] [-Path] <String[]>
[-LiteralPath] <String[]>
[-Filter <String>] [-Include <String[]>] [-Exclude <String[]>] [-Force] [-Credential <PSCredential>] [-Delimiter <String>] [-Wait] [-Raw] [-Encoding <Encoding>] [-AsByteStream] [-Stream <String>] [<CommonParameters>]

Parameters:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • Path: Path of the file to be read. You can provide one or more paths of the files but not the directories. Wildcard characters are permitted.
  • Read Count: Specifies the number of lines of the content sent through the pipeline at a time. The Default value is 1 means it sends one line at a time. If you set the value to 0 then it sends the whole content at a time. This parameter doesn’t change the content it displays but it does affect the time to display the content. The more the read count value is, the more time it will take to reach the first line but overall operation time decreases.
  • TotalCount: Specifies the total number of lines to display from the beginning. If you set the value 10, it will display 10 lines only. The default value is -1, which means all the lines will be displayed. Its aliases are Head and First.
  • Tail: Specifies the total number of lines to display from the end of the file. Its alias is Last.
  • Include: Include parameter is path qualifier means it includes all the items from that path. Wildcard character (*) is permitted. When you use –Include parameter then you need to provide content of the item. For example, D:\temp\* where (*) specifies the contents of the directory.
  • Exclude: Exclude parameter is path qualifier means it excludes all the items which are specified from that path. Wildcard character (*) is permitted. When you use –Exclude parameter then you need to provide content of the item. For example, D:\Temp\* where (*) specifies the contents of the directory.
  • Filter: The filter parameter is more efficient than include or exclude. It is also a path qualifier and you can use the wildcard character (*) for this parameter.
  • Force: Retrieves the contents from the file which is restricted by any settings except security permissions. Force parameter overrides the read-only attribute of the file or creates a directory to complete a file path.
  • Credential: When the file is located on a different domain or in Workgroup, you can use the credentials of the file of that location to retrieve its contents.
  • Delimiter: While retrieving the contents from the file, it uses a delimiter character to split the files into string objects. The default delimiter is (\n), end of the line character.
  • Wait: When the wait parameter is specified, the Powershell console keeps the file open and waits until manually interrupted by entering CTRL + C or by deleting the file. In the second case, there will be a non-terminating error. The wait cannot be combined with Raw Parameter.
  • Raw: Returns the multiple lines as a single string but preserves the new lines in the output.
  • Encoding: Specifies the type of encoding for the target file. Accepted encoding values.

powershell content

  • AsByteStream: This parameter was introduced in PowerShell 6.0 and specifies if the contents should be read as a byte of the stream.
  • Stream: This parameter is used to create the stream. Once you create a different stream, you can retrieve changes in the files according to the stream.
  • LiteralPath: This parameter specifies the path of one or more locations. Unlike -path parameter, you cannot specify wildcard characters here because this parameter can’t interpret characters as wildcards. If your path includes any escape characters then mark them under a single quote and PowerShell will consider it as a single path.
  • CommonParameters: Below common parameters are used which are also called advance function’s parameters.
    Verbose,Debug, ErrorAction, ErrorVariable, WarningAction, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable.

Examples of PowerShell Get-Content

The examples of the PowerShell Get-Content is given below:

Example #1

Read file through Get-Content.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,527 ratings)

In the testreadC.txt file, we have stored processes.

Get-Content -Path D:\Temp\testreadC.txt

Output:

powershell content

Example #2

GC with TotalCount

Get-Content -Path D:\Temp\testreadC.txt -TotalCount 10

A total of 10 lines will be displayed from the beginning.

Output:

powershell content

Example #3

GC with Tail

Get-Content -Path D:\Temp\testreadC.txt -Tail 10

A total of 10 lines will be displayed from the bottom.

Output:

powershell content

Example #4

GC with ReadCount

Get-Content -Path D:\Temp\testreadC.txt -ReadCount 10 | Set-Content D:\Temp\ReadC.txt

The above example will send 10 lines at a time to new file ReadC.txt.

Example #5

GC with Delimiter

Get-Content D:\Temp\delim.txt -Delimiter '@'

The above command will split the file content with ‘@’ character and start a new line after it.

For example,

powershell content

Example #6

GC with Raw

Get-Content D:\Temp\testreadC.txt -Raw

The above command will store the entire file content into a single string instead of an array. We can check the line count as below.

Output:

GC with Raw

Example # 7

GC with Encoding

Get-Content D:\Temp\testreadC.txt -Encoding Byte -TotalCount 10

Here, encoding Byte is selected so the data will be in byte format. You can select another formatting as mentioned above in parameter explanation.

Output:

GC with Encoding

Example #8

GC with Stream

Get-Content D:\Temp\stream1.txt -Stream $stream1

The above command will keep the existing content in the stream1. When new content is added with the new stream in the same file then it can be recognized easily.

Set-Content D:\Temp\stream1.txt -Value "This is the new line" -
Stream $stream2
Get-Content D:\Temp\stream1.txt -Stream $stream2

Output:

GC with Stream

Example #9

GC with Filter / Include / Exclude parameter.

Get-Content D:\Temp\* -Filter Testread*

The above command will read all the files that start with “TestRead” in the file name.

Get-Content D:\Temp\* -Include *read*

The above command will read all the files that contain “Read” in the file name.

Get-Content D:\Temp\* -Exclude Test*

The above command will exclude all the files that start with “Test” in the file name and will read content from other files on the same path.

Recommended Articles

This is a guide to PowerShell Get-Content. Here we discuss the introduction, Parameters for the given Syntax and the Examples of PowerShell Get-Content. You can also go through our other related articles to learn more–

  1. PowerShell Set-Location
  2. VI Editor in Unix
  3. cmdlets in PowerShell
  4. PowerShell Set-Content
  5. PowerShell Break | Top 8 Examples
  6. PowerShell Convert to String | Parameters
Popular Course in this category
All in One Data Science Bundle (360+ Courses, 50+ projects)
  360+ Online Courses |  1500+ Hours |  Verifiable Certificates |  Lifetime Access
4.7
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
Data Visualization Training (15 Courses, 5+ Projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more