Bookmark and Share

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I need to know how to use Windows PowerShell 2.0 to create a series of text files on my Windows 7 computer. Here is the deal: I would like to create a large number of text files that contain basic information and would be used for record keeping. Is this something that can be done with Windows PowerShell 2.0?

-- PR

 

 

Hey, Scripting Guy! AnswerHello PR,

Microsoft Scripting Guy Ed Wilson here. I have spent a lot of time reviewing the various scripts that were submitted during the 2010 Scripting Games, and as it turns out, Event 3 for both the Beginner and Advanced categories involved creating text files. For example, the BEG3_GreggB.ps1 script creates text files that have three fields in them: class, date, and notes. The script begins by using a for statement that loops around 10 times. The script block for the for statement uses the Out-File cmdlet to create the 10 text files. The -filepath parameter contains a hardcode path statement, so to create a file in a different location would require editing the script. Because an expanding string is used, the value contained in the $i variable is automatically used when creating the new file. The –force parameter is used to force the cmdlet to overwrite any text files that already exist with the same names. The cmdlet will overwrite a file even if it is read-only, but it will not override security restrictions. The `r`n is the special sequence that forces a carriage return and a line feed. The complete BEG3_GreggB.ps1 script is shown here.

BEG3_GreggB.ps1

for ($i = 1; $i -le 10; $i++) { Out-File -filepath "C:\fso\file$i.txt" `
-force -input "CLASS:`r`nDATE:`r`nNOTES:`r`n" }

The script produces no output other than the 10 text files that populate the c:\fso folder. One of the text files is shown in the following image.

Image of one of text files created

This script could have been written several other ways. The range operator can be used to create the numbers in the series that we need. In this example, we need the numbers 1 through 10. The range operator specifies the upper boundary and the lower boundary of an array. It then creates the numbers. You can use the range operator to create numbers in both directions. This is shown here:

PS C:\> 1..3

1

2

3

PS C:\> 3..1

3

2

1

PS C:\>

When you have an array of numbers, you can use the ForEach-Object cmdlet to work through the array of numbers. The alias for the ForEach-Object cmdlet is the percentage sign (%). The redirection arrow (>) is used to write content to a text file. The single redirection arrow will automatically overwrite a file if it exists. However, it does not overwrite a read-only file. An “Access denied” error will be generated if the previously existing file is read-only. The double redirection arrow appends to a file, if it exists. It also will generate an “Access denied” error if a previously existing file is read-only. The compact version of the script is shown here:

PS C:\> 1..10 | % { "CLASS:`r`nDATE:`r`nNOTES:`r`n" > "c:\fso\file$_.txt" }

PS C:\>

There are several reasons you might consider using the Out-File cmdlet instead of using the redirection arrows. The first reason is that if a file is read-only, an error will be generated. One problem with the generated error is that it states that access is denied, which might cause a user to waste time investigating security permissions instead of looking for the relatively simple read-only bit.

The second reason for using the Out-File cmdlet is to specify encoding (Unicode, etc.) for the file. A third reason, is the ability to specify the width of the text that gets written to the file. The last reason it that Out-File is easier to read and to understand than a redirection arrow. Jeffery Hicks—Microsoft MVP, author, and 2010 Scripting Games judge—has an interesting blog entry about the use of Out-File.

PR, that is all there is to using Windows PowerShell to create a series of text files. 2010 Scripting Games Wrap-Up Week will continue tomorrow.

 

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson and Craig Liebendorfer, Scripting Guys