C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
PowerShell Set-Content | PowerShell write to fileThe 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. SyntaxSet-Content [-Path] <string[]> [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-Value] <Object[]> [-PassThru] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-UseTransaction] [-NoNewline] [-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem | BigEndianUTF32}] [-Stream <string>] [<CommonParameters>] Set-Content [-Value] <Object[]> -LiteralPath <string[]> [-PassThru] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [-NoNewline] [-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem |BigEndianUTF32}] [-Stream <string>] [<CommonParameters>] 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:
-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. ExamplesExample 1: Create a New file and write 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. 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 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.
Next TopicPowerShell Out-File
|