Working with files
Using PowerShell to read the contents of files and manipulate that data is a common task. We’re going to look at three cmdlets in this section. First, let’s explore the generic Get-Content
cmdlet.
Get-Content
The Get-Content
cmdlet can get any type of data in a file and store it as an array of strings or bytes (as a bytestream). By default, each line in the file will be interpreted as a single string. We will only get meaningful content if the file can be interpreted as an array of strings; otherwise, we must fetch it as a bytestream. This is useful if we are getting the contents of an executable file, for example. It’s got a few parameters. Here are the more interesting ones:
-Path
specifies the item we want. This accepts an array of string values so that we can concatenate multiple files into a single array.-TotalCount
is the total number of lines to read from the file. The default is-1
, which will read to the last line...