Out-File

 

Additional Resources for Out-File

 

Saving Data Directly to a Text File

http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/out-file.mspx

 

 

SYNOPSIS

Sends output to a file.

 

SYNTAX

Out-File [-filePath] <string> [[-encoding] <string>] [-append] [-width <int>] [-inputObject <psobject>] [-force] [-noClobber] [-whatIf] [-confirm] [<CommonParameters>]

 

DETAILED DESCRIPTION

The Out-File cmdlet sends output to a file. You can use this cmdlet instead of the redirection operator (>) when you need to use its parameters.

 

PARAMETERS

 

-filePath <string>

Specifies the path to the output file.

 

Required?

true

Position?

1

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-encoding <string>

Specifies the type of character encoding used in the file. Valid values are "Unicode", "UTF7", "UTF8", "UTF32", "ASCII", "BigEndianUnicode", "Default", and "OEM". "Unicode" is the default.

 

·          "Default" uses the encoding of the system's current ANSI code page.

·          "OEM" uses the current original equipment manufacturer code page identifier for the operating system.

 

Required?

false

Position?

2

Default value

Unicode

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-append <SwitchParameter>

Adds the output to the end of an existing file, instead of replacing the file contents.

 

Required?

false

Position?

named

Default value

False

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-width <int>

Specifies the number of characters in each line of output. Any additional characters are truncated, not wrapped. If you omit this parameter, the width is determined by the characteristics of the host. The default for the PowerShell.exe host is 80 (characters).

 

Required?

false

Position?

named

Default value

80

Accept pipeline input?  

false

Accept wildcard characters?  

false

 

-inputObject <psobject>

Specifies the objects to be written to the file. Enter a variable that contains the objects or type a command or expression that gets the objects.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

true (ByValue)

Accept wildcard characters? 

false

 

-force <SwitchParameter>

Overrides restrictions that prevent the command from succeeding, just so the changes do not compromise security. For example, Force will override the read-only attribute or create directories to complete a file path, but it will not attempt to change file permissions.

 

Required?

false

Position?

named

Default value

false

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-noClobber <SwitchParameter>

Will not overwrite (replace the contents) of an existing file. By default, if a file exists in the specified path, Out-File overwrites the file without warning. If both Append and NoClobber are used, the output is appended to the existing file.

 

Required?

false

Position?

named

Default value

False

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-whatIf

Describes what would happen if you executed the command without actually executing the command.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-confirm

Prompts you for confirmation before executing the command.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. For more information, type, "get-help about_commonparameters".

 

INPUT TYPE

Any

 

RETURN TYPE

None

 

NOTES

 

For more information, type "Get-Help Out-File -detailed". For technical information, type "Get-Help Out-File -full".

 

When specifying multiple values for a parameter, use commas to separate the values. For example, "<parameter-na me> <value1>, <value2>".

 

The Out cmdlets do not format objects; they just render them and send them to the specified display destination. If you send an unformatted object to an Out cmdlet, the cmdlet sends it to a formatting cmdlet before rendering it.

 

The Out cmdlets do not have parameters for names or file paths. To send data to an Out cmdlets, use a pipeline operator (|) to send the output of a Windows PowerShell command to the cmdlet. You can also store data in a variable and use the InputObject parameter to pass the data to the cmdlet. For help, see the examples.

 

Out-File sends data, but it does not emit any output objects. If you pipe the output of Out-File to Get-Member, Get-Member reports that no objects have been specified.

 

EXAMPLE 1

 

get-process | out-file -filepath C:\Test1\process.txt

 

This command sends a list of processes on the computer to the Process.txt file. If the file does not exist, Out-File creates it. Because the name of the FilePath parameter is optional, you can omit it and submit the equivalent command " get-process| outfile C:\Test1\process.txt".

 

EXAMPLE 2

 

get-process | out-file C:\Test1\process.txt -noclobber

 

This command also sends a list of processes to the Process.txt file, but it uses the NoClobber parameter, which prevents an existing file from being overwritten. The output shows the error message that appears when NoClobber is used with an existing file.

 

Out-File : File C:\Test1\process.txt already exists and NoClobber was specified.

At line:1 char:23

+ get-process | out-file  <<<< process.txt -noclobber

 

EXAMPLE 3

 

$a = get-process

 

out-file -filepath C:\Test1\process.txt -inputobject $a -encoding ASCII -width 50

 

These commands send a list of processes on the computer to the Process.txt file.

 

The first command gets the list of processes and stores them in the $a variable. The second command uses the Out-File cmdlet to send the list to the Process.txt file.

 

The command uses the InputObject parameter to specify that the input is in the $a variable. It uses the Encoding parameter to convert the output to ASCII format. It uses the Width parameter to limit each line in the file to 50 characters. Because the lines of output are truncated at 50 characters, the rightmost column in the process table is omitted.

 

EXAMPLE 4

 

set-location hklm:\software

 

get-acl mycompany\mykey | outfile -filepath c:\ps\acl.txt

 

get-acl mycompany\mykey | outfile -filepath filesystem::acl.txt

 

These commands show how to use the Out-File cmdlet when you not in a FileSystem drive.

 

The first command sets the current location to the HKLM:\Software registry key.

 

The second and third commands have the same effect. They use the Get-Acl cmdlet to get the security descriptor of the MyKey registry subkey (HKLM\Software\MyCompany\MyKey). A pipeline operator passes the result to the Out-File cmdlet, which sends it to the Acl.txt file.

 

Because Out-File is not supported by the Windows PowerShell Registry provider, you must specify either the file system drive name, such as "c:", or the name of the provider followed by two colons, "FileSystem::" in the value of the FilePath parameter. The second and third commands demonstrate these methods.

 

RELATED LINKS

Out-String

Out-Null

Out-Host

Out-Printer

Out-Default

Tee-Object