Introduction to PowerShell Out-File
PowerShell Out-file command used to store or capture output to any file. Many times we may want to see all the errors and output, so storing them into a file along with its date of creation is a better idea as compared to simply printing it onto the console. Because the size of the output can be very large, so in that case, we can send output to the file is better.
[-FilePath] <String path of file for capturing output>
[[-Encoding] <Encoding types like ASCII,UTF8 etc>] [-Append<add output to existing file at the end>] [-Force<append output to file even read only access is there>] [-NoClobber] [-Width <int value of number of characters in one line>] [-NoNewline] [-InputObject <psobject>] [-WhatIf<What will happen on execution of command>] [-Confirm<Yes or No confirmation before execution of command>] [<CommonParameters>]
[[-Encoding] <Encoding types like ASCII,UTF8 etc>]
-LiteralPath <string exact target file path>
[-Append<add output to existing file at the end>] [-Force<append output to file even read only access is there>] [-NoClobber] [-Width <int value of number of characters in one line>] [-NoNewline] [-InputObject <psobject>] [-WhatIf<What will happen on execution of command>] [-Confirm<Yes or No confirmation before execution of command>] [<CommonParameters>]
Below are some of the parameters given.
1. -Append: In case we wanted to add any output to any existing file then we can use this command. This command will add your output to the end of any existing file.
2. -Confirm: This command we can use if we want to make safe command execution. Here it will display a prompt box that will ask for your confirmation for the execution of the command.
3. -Encoding: This command defines the type of encoding for the file where we are going to write our content. If we do not define the type then it will take UTF8NoBOM as the default value of type.
There are several types accepted by it, they are given below:
- ASCII: In this type, it will Use ASCII 7 bit character. This type of encoding is very common.
- BigEndianUnicode: It uses UTF-16 format to encode. It uses big-endian with UTF-16.
- OEM: In this case, it will use MS-DOS default encoding and the console program
- Unicode: It uses the UTF-16 format to encode. But it uses little-endian with UTF-16.
- UTF-7: In this type encoding done in UTF-7 format.
- UTF8: In this type encoding done in UTF-8 format.
- UTF8BOM: In this type encoding done in UTF 8 BOM format. It uses Byte Order marks with UTF 8 BOM.
- UTF8NoBOM: In this type encoding done in UTF 8 BOM format. It does not use Byte order marks with UTF 8 BOM.
- UTF32: In this type encoding done in UTF-32 format.
- If we are using PowerShell version 6.2 or above then we can also use the numeric ID of any register code page for example -Encode 1251. Here 1251 is page number ID. We can use a string name of the register code page for example -Encode “Windows – 1251”.
4. -FilePath: This command defines the path for output file where we want to capture or send the output(error or output of events).
5.-Force: Suppose there are any files with read-only access than in that case -Force command will allow to overwrite on it. Remember the force parameter will only be able to write in case of read-only access on file, it can not overwrite security restricts made on that file. This means it can not compromise with security.
6. -InputObject: Defines the Object which is going to be written on the file. We can take any variable that contains the Object, Type, or any command or expression which will return any Object(Object as output).
7. -LiteralPath: Here we define the path of the file for output capturing. We write the literal path when know the exact path, we can not pass wildcard in it. For example, we have to pass “/desktop/ranjan/test.txt” if we will try to use a path like “*.txt” it would not work. If we can use escape character for but in that case, we need to enclose it with a single quotation mark. By using a single quotation we are notifying to PowerShell that this is an escape character.
8. -NoNewline: In case if we do not want to add a new line character at the end of the file then we can use this command.
9. -WhatIf: Display what will happen on the execution of the command, it will make you clear about the purpose of the command which you are going to run without actual execution.
10. -Width: It defines how many characters can be there on each line of output. If there will be any extra character than that will be truncated. In case you will not use this attribute, it will define this value on the basis of the host. In general, PowerShell can accommodate 80 characters.
Examples of PowerShell Out-File
Given below are the examples of PowerShell Out-File:
In this example we are trying to capture all the process into a file, we can see we are passing “.\captureprocess.txt” as an argument to -FilePath as a command, which will capture all the process and on writing cat ./captureprocess.txt file contains all the process names.
Get-Process | Out-File -FilePath .\captureprocess.txt
In this example, we are capturing all the child items (files and folders) of the current folder containing file ./capturechilditem.txt file.
Get-ChildItem | Out-File -FilePath .\capturechilditem.txt
In this example, we are capturing output to the file capturechilditem.txt, but we are using command -Confirm, this command will generate a prompt and it will ask for your confirmation before the execution of the command.
Get-Process | Out-File -FilePath .\capturechilditem.txt -Confirm
In this command, we are using -WhatIf and you can see the output without real execution of the command. In simple words, it is a trial for running the command, without affecting anything. We use this command to understand the purpose of execution of the command before actual execution.
Get-Process | Out-File -FilePath .\capturechilditem.txt -WhatIf
Here we are using -Force command along with sending output to the file capturechilditem.txt, here we are assuming that this file has only read access. Here Force will be used to write output to the file. Force can not bypass the security criteria.
Get-Process | Out-File -FilePath .\capturechilditem.txt -Force
From the above tutorial we learned that with the help of Out-File command we can capture(send) the output of the file, This output can be errors or it can be any event or activity.
This has been a guide to PowerShell Out-File. Here we discuss the introduction, parameters, and examples of PowerShell Out-File. You may also have a look at the following articles to learn more –