PowerShell Get-Content Cmdlet
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Add-Content cmdlet in PowerShell.
https://cloudaffaire.com/powershell-add-content-cmdlet/
In this blog post, we will discuss Get-Content cmdlet in PowerShell. You can use Get-Content cmdlet to read from a file in PowerShell. The Get-Content cmdlet gets the content of the file specified by the path parameter. You can get content of multiple files at the same time using Get-Content cmdlet.
Get-Content Cmdlet Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## Get-Content ## [-ReadCount ## [-TotalCount ## [-Tail ## [-Path] ## [-Filter ## [-Include ## [-Exclude ## [-Force] ## [-Credential ## [-Delimiter ## [-Wait] ## [-Raw] ## [-Encoding ## [-Stream ## [ |
Get-Content Cmdlet Argument List:
- –Credential: Credentials to impersonate another user, or elevate your credentials when running this cmdlet.
- –Delimiter: Specifies the delimiter that Get-Content uses to divide the file into objects while it reads. The default is \n, the end-of-line character.
- –Encoding: Specifies the type of encoding for the target file. The default value is Default.
- –Exclude: Specifies, as a string array, an item or items that this cmdlet excludes in the operation. The value of this parameter qualifies the Path parameter.
- –Filter: Specifies a filter to qualify the Path parameter. The FileSystem provider is the only installed PowerShell provider that supports the use of filters.
- –Force: Force will override a read-only attribute or create directories to complete a file path. The Force parameter does not attempt to change file permissions or override security restrictions.
- –Include: Specifies, as a string array, an item or items that this cmdlet includes in the operation. The value of this parameter qualifies the Path parameter.
- –LiteralPath: Specifies a path to one or more locations. The value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards.
- –Path: Specifies the path to an item where Get-Content gets the content. Wildcard characters are permitted.
- –Raw: Ignores newline characters and returns the entire contents of a file in one string with the newlines preserved.
- –ReadCount: Specifies how many lines of content are sent through the pipeline at a time. The default value is 1. A value of 0 (zero) sends all of the content at one time.
- –Stream: Gets the contents of the specified alternate NTFS file stream from the file. Enter the stream name. Wildcards are not supported.
- –Tail: Specifies the number of lines from the end of a file or other item. You can use the Tail parameter name or its alias, Last.
- –TotalCount: Specifies the number of lines from the beginning of a file or other item. The default is -1 (all lines).
- –UseTransaction: Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
- –Wait: Keeps the file open after all existing lines have been output. While waiting, Get-Content checks the file once each second and outputs new lines if present.
PowerShell Get-Content Cmdlet:
Get Content Of A File In PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
####################################### ## PowerShell | Cmdlet | Get-Content ## ####################################### ## PowerShell Latest Version (5) ## create a directory and two files and add some data to it New-Item -ItemType Directory -Force -Path C:\MyDir\ New-Item -ItemType file -path "C:\MyDir\myfile1.txt", ` "C:\MyDir\myfile2.txt", "C:\MyDir\myfile3.txt" 1..5 | ForEach-Object { Add-Content -Path C:\MyDir\myfile1.txt -Value "$_" } Add-Content -Path C:\MyDir\myfile2.txt -Value "AWS,GCP,Azure,Cloud,Python,PowerShell" Add-Content -Path C:\MyDir\myfile3.txt -Value "A`r`nB`r`nCloudAffaire`r`nD`r`nE" ## get content of a file in PowerShell Get-Content -Path C:\MyDir\myfile1.txt |
Get Contents Of Multiple Files In PowerShell:
1 2 3 |
## get contents of multiple files in PowerShell Get-Content -Path C:\MyDir\*.txt |
Get Contents Of Multiple Files Excluding Some Files In PowerShell:
1 2 3 |
## get content of multiple files excluding some file in PowerShell Get-Content -Path C:\MyDir\*.txt -Exclude myfile2.txt |
Get Contents Of Multiple Files Along With File Names In PowerShell:
1 2 3 4 5 6 7 8 9 |
## get content of multiple files along with filename in PowerShell Get-ChildItem 'C:\MyDir' | ForEach-Object { @' {0} ----------- {1} '@ -f $_.Name, (Get-Content $_.FullName -Raw) } |
Get Specific Number Of Lines From A File In PowerShell:
1 2 3 4 5 6 7 |
## get specific numbers of lines from a file in PowerShell Get-Content -Path C:\MyDir\myfile1.txt -TotalCount 3 Get-Content -Path C:\MyDir\myfile1.txt -Tail 2 (Get-Content -Path C:\MyDir\myfile1.txt -TotalCount 4)[-1] |
Change The Delimiter From Default New Line To Some Other In PowerShell:
1 2 3 |
## change the delimiter from new line to other in get-content Get-Content -Path C:\MyDir\myfile2.txt -Delimiter "," |
Get A Line Containing Specific Pattern From A File In PowerShell:
1 2 3 4 5 6 7 |
## get content of a file containing specific pattern in PowerShell Get-Content -Path C:\MyDir\myfile3.txt | Select-String -Pattern "CloudAffaire" ## remove the directory along with content Remove-Item -Force -Recurse C:\MyDir\ |
Hope you have enjoyed this article. In the next blog post, we will discuss Set-Content cmdlet in PowerShell.
To get more details on PowerShell, kindly follow below official documentation