Export (0) Print
Expand All
Expand Minimize

Converting the FileSystemObject's Read Method


Windows PowerShell


Definition: Reads a specified number of characters from a file, from the beginning of the file.

Read


PowerShell doesn’t have an equivalent cmdlet or method, but you can use the Get-Content cmdlet to do the same type of thing.

This example uses the -totalcount parameter to read only the first line of the file, then uses the syntax [0..4] to retrieve array elements 0 through 4 from the resulting string - in other words, the first 5 characters of the string:


(Get-Content C:\Scripts\test.txt -totalcount 1)[0..4]


Note that this returns the results as an array rather than a string.

What if we wanted to read the whole file, but retrieve only the first 5 characters? We can use the same command we just showed (without the -totalcount parameter), but we run into issues if there are fewer than 5 characters in the first line. In this next example, we’re creating a StreamReader object which references the file Test.txt. We then use the ReadToEnd method to read the entire contents of the file into the variable $b. At that point we can use the Substring method to read the number of characters we’re looking for. The first parameter to Substring is the starting position. We want to start at the beginning of the string, so we use a position index of 0. The second parameter is the number of characters we want to return, in this case 5.


$a = New-Object -typename System.IO.StreamReader -argumentlist "C:\scripts\test.txt"
$b = $a.ReadToEnd()
$c = $b.substring(0,5)
$c
$a.Close()


If your file contained the following text:


T1
Test1


Your output would look like this:


T1
T


Notice that only three characters are returned. That’s because the carriage-return linefeed characters are included. This is the same results you’ll see if you call the FileSystemObject’s Read method on this same content. However, if you don’t want to include these characters, simply replace all carriage-return linefeeds in the content string before looking for your substring:


$a = New-Object -typename System.IO.StreamReader -argumentlist "C:\scripts\test.txt"
$b = $a.ReadToEnd()
$b = $b -replace("`n","")
$c = $b.substring(0,5)
$c
$a.Close()


This gets us a little closer:


T1Te


One important thing to notice in the examples where we used the StreamReader object is the call to the Close method at the end. If you don’t close your StreamReader object it will remain open, which means you won’t be able to edit the file your read from anywhere else until you either close the stream or exit PowerShell.

And just for good measure, here’s one more option for reading from a text file. You could always just use the FileSystemObject:


$a = New-Object -ComObject Scripting.FileSystemObject
$f = $a.OpenTextFile("C:\scripts\test2.txt", 1)
$f.Read(4)


See conversions of other FileSystemObject methods and properties.
Return to the VBScript to Windows PowerShell home page

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft