Reading content from files is one part of using files for administration; sending output to files is another. This recipe is intended to show you the options available to send output to files. In PowerShell, the process works through redirection. We will mainly look at the following:
- The Out-File cmdlet
- The Tee-Object cmdlet
- Redirection operators
The scenario for this recipe is as follows:
- Send the list of the contents of ~/random/dir-04 to a file called ~/random/file-list.txt.
- Try to list the contents of ~/random/dir-05. This would throw an error (because there is no such directory as ~/random/dir-05). This error must be sent to ~/random/error.txt.
- Use Write-Host to write a note that the list of contents of the directory, ~/random/dir-03, is being appended to ~/random/file-list.txt; this console message should go to ~/random/message.txt.
- Append the...