PowerShell Set-Content Cmdlet

PowerShell Set-Content Cmdlet

PowerShell Set-Content Cmdlet

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed Get-Content cmdlet in PowerShell.

https://cloudaffaire.com/powershell-get-content-cmdlet/

In this blog post, we will discuss Set-Content cmdlet in PowerShell. You can use Set-Content cmdlet to write new content or replace existing contents in a file. Set-Content cmdlet and Add-content cmdlet both can be used to write contents to a file but Set-Content cmdlet always replaces existing content whereas Add-Content cmdlet always appends to the existing content. You can use “Value” argument to provide the content of the file.

Set-Content Cmdlet Syntax:

Set-Content Cmdlet Argument List:

  • Confirm: Prompts you for confirmation before running the cmdlet.
  • Credential: Impersonate another user, or elevate your credentials when running this cmdlet.
  • Encoding: Specifies the type of encoding for the target file. The default value is Default.
  • Exclude: Specifies, as a string array, an item or items that this cmdlet excludes in the operation. The value of this parameter qualifies the Path parameter.
  • Filter: Specifies a filter to qualify the Path parameter.
  • Force: Forces the cmdlet to set the contents of a file, even if the file is read-only.
  • Include: Specifies, as a string array, an item or items that this cmdlet includes in the operation. The value of this parameter qualifies the Path parameter.
  • LiteralPath: Specifies a path to one or more locations. The value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards.
  • NoNewline: The string representations of the input objects are concatenated to form the output. No spaces or newlines are inserted between the output strings.
  • PassThru: Returns an object that represents the content. By default, this cmdlet does not generate any output.
  • Path: Specifies the path of the item that receives the content. Wildcard characters are permitted.
  • Stream: Specifies an alternative data stream for content. If the stream does not exist, this cmdlet creates it. Wildcard characters are not supported.
  • UseTransaction: Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
  • Value: Specifies the new content for the item.
  • WhatIf: Shows what would happen if the cmdlet runs. The cmdlet is not run.

PowerShell Set-Content Cmdlet:

Write Some Data To A File In PowerShell:

Write Some Data With New Line Character To A File Overwriting Existing Content In PowerShell:

Write Some Data To Multiple Files At Once In PowerShell:

Write Some Data To Multiple Files At Once Excluding Some Files In PowerShell:

Write Some Data To A Read-Only File In PowerShell:

Read Content Of A File And Write To Another File In PowerShell:

Write Output Of A Cmdlet To A File In PowerShell:

Hope you have enjoyed this article. In the next blog post, we will discuss Clear-Content cmdlet in PowerShell.

To get more details on PowerShell, kindly follow below official documentation

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about?view=powershell-5.1

 

Leave a Reply

Close Menu