Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to work with text files.
Microsoft Scripting Guy, Ed Wilson, is here. Good morning! Today I have an excerpt from my new book, Windows PowerShell 3.0 First Steps, which was recently published by Microsoft Press.
One of the easiest methods to store data is in a text file. In the following image, the output from the Get-Volume function displays in the Windows PowerShell console. The output formats nicely in columns, and it contains essential information about the volumes on a Windows 8 laptop.
Redirect and append
The easiest way to store volume information that is obtained from the Get-Volume function is to redirect the output to a text file. Because several lines of information return from the function, it is best to redirect and append the outputted information. The redirect and append operator is two right arrows, one behind the other with no space between them.
The following command redirects and appends the information from the Get-Volume function to a text file that resides in the folder c:\fso. The file, VolumeInfo.txt, does not have to exist. If it does not exist, it will be created, and the information is written to the file. If the file does exist, the outputted data will append to the file. The command is shown here:
When the command runs, nothing outputs to the Windows PowerShell console. The output, formatted as it appears in the Windows PowerShell console, writes to the target text file. The following image shows the volumeinfo.txt file that was created by redirecting and appending the results of the Get-Volume function from Windows 8.
If you run the command that redirects and appends the information from the Get-Volume function to a text file that resides in the folder c:\fso named volumeinfo.txt a second time, the information from Get-Volume writes to the bottom of the previously created text file—that it, it appends to the file. This is a great way to produce simple logging. The following image shows the volume information appearing twice. In both cases, the values are identical. This shows that between the first time the Get-Volume command ran and the second time Get-Volume ran, nothing changed.
Redirect and overwrite
If you do not need to maintain a history of prior settings, results, or data, use the redirect operator and do not append. The redirect and overwrite operator is a single right arrow. The following command redirects and overwrites the information from the Get-Volume function to a text file that resides in the folder c:\fso. The file, VolumeInfo.txt, does not have to exist. If it does not exist, it will be created, and the information is written to the file. If the file does exist, the outputted data will overwrite previously existing data when writing to the file. The command is shown here:
Comparing the SizeRemaining value of drive C from the following image with the SizeRemaining value of the drive C in the previous image reveals that the drive suddenly has nearly 4 GB of additional free space. But because the volumeinfo.txt file is overwritten by the redirect and overwrite operator, you would have no way to discover this condition unless you had a backup of the previous volumeinfo.txt file. Knowing when 4 GB of disk space suddenly became available might help the Help Desk when a user calls and says, “All of a sudden, Outlook is not working.”
Join me tomorrow when I will have another excerpt from my Microsoft Press book, Windows PowerShell 3.0 First Steps.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy