Javatpoint Logo
Javatpoint Logo

PowerShell Get-Content

The PowerShell Get-Content cmdlet gets the content of an item at a specified location. The gc, cat, and type are the aliases of this cmdlet.

Syntax


Parameters

-Filter

The -Filter parameter is used to specify a filter to qualify the -Path parameter. The FileSystem provider is the only PowerShell provider that supports the uses of filters. This parameter is more efficient as the provider applies the filters when the cmdlet gets the object, rather than having Powershell filters the object after they are accessed.

-Include

The items that this cmdlet includes in the operation are specified as a string array. The value of -Include parameter qualifies the -Path parameter. Enter a pattern or a path element, such as *.txt. The characters of wildcard are permitted. It is effective only when the cmdlet includes the contents of an item, such as C:\*, the wildcard character '*' is used to specify the contents of the C: directory.

-Exclude

The items that this cmdlet excludes in operation are specified as a string array. The value of -Exclude parameter qualifies the -Path parameter. Enter a pattern or a path element, such as *.txt. The characters of wildcard are accepted. The -Exclude parameter is effective only when the cmdlet includes the contents of an item, such as C:\*, the wildcard character '*' is used to specify the contents of the C: directory.

-Force

The -Force parameter will override the read-only attribute or creates the directories to complete a file path. It does not attempt to change the file permissions or override the security permissions.

-Path

The -Path parameter is used to specify the path to an item where this cmdlet gets the content. Wildcard characters are accepted. The paths must be the paths to the items, not to containers.

-ReadCount

The -ReadCount parameter is used to specify how many lines of the content are sent through the pipeline at a time. It does not change the content to be displayed, but it affects the time taken to display the content.

-TotalCount

The -TotalCount parameter is used to specify the number of lines from the beginning of the file. We can also use the aliases First or Head of this parameter.

-Tail

The -Tail parameter is used to specify the number of lines from the end of the file. It was introduced in Windows PowerShell 3.0. We can also use the alias Last of this parameter.

-Delimiter

The -Delimiter parameter is used in this cmdlet to divide the file into objects while it reads. We can use this parameter to split the large files into smaller files by specifying a file separator, as a delimiter.

It is preserved and becomes the last item in the section of each file. It is a dynamic parameter which is added by the FileSystem provider to the Get-Content cmdlet.

-Wait

The -Wait parameter is used to keep the files open after all the existing lines have been output.

It is a dynamic parameter which is added by the FileSystem provider to the Get-Content cmdlet. This parameter cannot be combined with the -Raw parameter.

-Raw

The -Raw parameter is used to ignore the newline characters and returns the whole content of a file in a single string. In the file, the newline characters are used as delimiters by default to separate the inputs into an array of string. It was introduced in Windows PowerShell 3.0.

It is a dynamic parameter which is added by the FileSystem provider to the Get-Content cmdlet

-Encoding

The -Encoding parameter is used to specify the type of encoding for the target file. Its default value is UTF8NoBOM.

Following are the acceptable values for this parameter:

  • ASCII: This value uses the encoding for the ASCII (7-bit) character set.
  • Unicode: It encodes in UTF-16 format using the little-endian byte order.
  • UTF7: It encodes in UTF-7 format.
  • UTF8: It encodes in UTF-8 format.
  • BigEndianUnicode: It encodes in UTF-16 format using the big-endian byte order.
  • OEM: It uses the default encoding for MS-DOS and console programs.
  • UTF8NoBOM: It encodes in UTF-8 format without Byte Order Mark (BOM)
  • UTF32: It encodes in UTF-32 format.
  • UTF8BOM: It encodes in UTF-8 format with Byte Order Mark (BOM)

It is a dynamic parameter which is added by the FileSystem provider to the Get-content cmdlet. It is available only in the drives of a file system.

-Stream

The -Stream parameter is used to get the content of the specified alternative NTFS file stream from the file. Wildcard characters are not supported. It was introduced in Windows PowerShell 3.0.

It is a dynamic parameter which is added by the FileSystem provider to the Get-content cmdlet and works only in the file system drives.

Examples

Example1: To get the content of a text file

PowerShell Get-Content

The cmdlet in this example is used to display the content of an np.txt file on the PowerShell console in the current directory.

Example2: To get the content of first n lines from a text file

PowerShell Get-Content

The cmdlet in this example displays the specific number of lines from the text file. The -Totalcount parameter displays the first 10 lines of content.

Example3: To get the specific line of content from a text file

PowerShell Get-Content

The cmdlet in this example is used to display the specific line of that content.

Example4: To get the last line of content from a text file

PowerShell Get-Content

The cmdlet in this example displays the last line of the content from the file. The -Tail parameter gets the last line of the file.






Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA