PowerShell Set-Content | PowerShell write to file

The PowerShell Set-Content cmdlet writes the new content or replacing the existing content in a file. It differs from the Add-content cmdlet, which appends the content to the file. To send the content to the Set-Content cmdlet, we can use the -Value parameter on the command line, or we can send a content through the pipeline. The sc is the alias for this cmdlet.

Syntax

Parameters

-Path

The -Path parameter is used to specify the path of an item that receives the content. Wildcard characters are accepted.

-LiteralPath

The -LiteralPath parameter is used to specify a path to one or more locations. If the path includes the escape characters, enclose it in single quotation marks. Single quotation mark tells the Windows PowerShell that it should not interpret any character as an escape sequence. There is no character in the cmdlet, which is interpreted as a wildcard.

-Force

The -Force parameter is used to force the cmdlet to set the content of a read-only file. It does not override the security permissions.

-WhatIf

The -WhatIf parameter is used to display what would happen if the cmdlet executes. The cmdlet does not execute.

-Confirm

The -confirm parameter is used to prompt a confirmation before running the cmdlet.

-Value

The -Value parameter is used to specify the new content for an item.

-PassThru

The -PassThru parameter is used to return an object which represents the content. By default, it does not generate any output.

-Filter

The -Filter parameter is used to specify a filter to qualify the -Path parameter. The FileSystem provider is the only PowerShell provider that supports the uses of filters. This parameter is more efficient as the provider applies the filters when the cmdlet gets the object, rather than having Powershell filters the object after they’re accessed.

-Include

The items that this cmdlet includes in the operation are specified as a string array. The value of -Include parameter qualifies the -Path parameter. Enter a pattern or a path element, such as *.txt. Wildcard characters are accepted. The -Exclude parameter is effective only when the cmdlet includes the contents of an item, such as C:*, the wildcard character ‘*‘ is used to specify the contents of the C: directory.

-Exclude

The items that this cmdlet excludes in operation are specified as a string array. The value of -Exclude parameter qualifies the -Path parameter. Enter a pattern or a path element, such as *.txt. Wildcard characters are accepted. The -Exclude parameter is effective only when the cmdlet includes the contents of an item, such as C:*, the wildcard character ‘*‘ is used to specify the contents of the C: directory.

-NoNewLine

There are no newlines or spaces inserted between the input string, and no newline is added after the last output string.

-Encoding

The -Encoding parameter is used to specify the type of encoding for the target file. Its default value is UTF8NoBOM. It is a dynamic parameter which is added by the FileSystem provider to the Set-Content cmdlet. It works only in the drives of the file system.

Following are the acceptable values for this parameter:

  • ASCII: It uses the encoding for the ASCII (7-bit) character set.
  • Unicode: It encodes in UTF-16 format using the little-endian byte order.
  • UTF7: It encodes in UTF-7 format.
  • UTF8: It encodes in UTF-8 format.
  • BigEndianUnicode: It encodes in UTF-16 format using the big-endian byte order.
  • OEM: It uses the default encoding for MS-DOS and console programs.
  • UTF8NoBOM: It encodes in UTF-8 format without Byte Order Mark (BOM)
  • UTF32: It encodes in UTF-32 format.
  • UTF8BOM: It encodes in UTF-8 format with Byte Order Mark (BOM)

-Stream

The -Stream parameter is used to specify an alternate data stream for the content. If the stream does not exist, then this cmdlet creates it. Wildcard characters are not accepted.

This parameter was introduced in Windows PowerShell 3.0.

It is a dynamic parameter which is added by the FileSystem provider to the Set-Content cmdlet. It works only in the drives of the file system.

Examples

Example 1: Create a New file and write content

PowerShell Set-Content

The first cmdlet in this example creates a new file and write the Windows Operating system to the file. It uses the -Path and -Value parameter to create a new file named pw.txt in the current directory.

The second cmdlet, in this example, uses the Get-content cmdlet to display the content of the pw.txt file in the PowerShell console.

Example 2: Replace the content of an existing file in a directory.

PowerShell Set-Content

The cmdlet, in this example, replaces the content of a file in a current directory. This cmdlet uses the -Value parameter, which provides the text string Hello PowerShell..!! that replaces the existing content in the file.

Example 3: Create a New file and write the system date and time to the file

PowerShell Set-Content

The first cmdlet in this example creates a new file and writes the current date and time of a system to the file in the current directory.

The Set-content cmdlet uses the -Path and -Value parameter to create a new file named dt.txt in the current directory. The -value parameter uses the Get-date cmdlet to get the current date and time.

The second cmdlet, in this example, uses the Get-Content cmdlet to display the content of the dt.txt file in the PowerShell console.