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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
## Set-Content ## [-Path] ## [-Value] ## [-PassThru] ## [-Filter ## [-Include ## [-Exclude ## [-Force] ## [-Credential ## [-WhatIf] ## [-Confirm] ## [-NoNewline] ## [-Encoding ## [-Stream ## [ |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
####################################### ## PowerShell | Cmdlet | Set-Content ## ####################################### ## PowerShell Latest Version (5) ## create a directory and some files and a function to get content of the files along with filename New-Item -ItemType Directory -Force -Path C:\MyDir\ New-Item -ItemType file -path "C:\MyDir\myfile1.txt","C:\MyDir\myfile2.txt",` "C:\MyDir\myfile3.txt","C:\MyDir\myfile4.txt","C:\MyDir\myfile5.ps1","C:\MyDir\myfile6.ps1" Set-ItemProperty -Path C:\MyDir\myfile4.txt -Name IsReadOnly -Value $True function get_content() { Get-ChildItem 'C:\MyDir' | ForEach-Object { @' ..........{0}.......... {1} '@ -f $_.Name, (Get-Content $_.FullName -Raw) } } ## add some data to a file in PowerShell Set-Content -Path C:\MyDir\myfile1.txt -Value 'Hello World' get_content |
Write Some Data With New Line Character To A File Overwriting Existing Content In PowerShell:
1 2 3 4 5 |
## add some data with a newline to a file in PowerShell Set-Content -Path C:\MyDir\myfile1.txt -Value "AWS`r`nGCP`r`nAzure" get_content |
Write Some Data To Multiple Files At Once In PowerShell:
1 2 3 4 5 |
## add some data to multiple files at once in PowerShell Set-Content -Path C:\MyDir\*.ps1 -Value 'Debjeet' get_content |
Write Some Data To Multiple Files At Once Excluding Some Files In PowerShell:
1 2 3 4 5 |
## add some data to multiple files excluding some files in PowerShell Set-Content -Path C:\MyDir\*.txt -Value 'CloudAffaire' -Exclude myfile4.txt get_content |
Write Some Data To A Read-Only File In PowerShell:
1 2 3 4 5 |
## add some data to a read-only file in PowerShell Set-Content -Path C:\MyDir\myfile4.txt -Value 'PowerShell' -Force get_content |
Read Content Of A File And Write To Another File In PowerShell:
1 2 3 4 5 |
## add content to a file by reading from another file in PowerShell Get-Content -Path C:\MyDir\myfile1.txt | Set-Content -Path C:\MyDir\myfile4.ps1 get_content |
Write Output Of A Cmdlet To A File In PowerShell:
1 2 3 4 5 6 7 8 9 |
## write output of a cmdlet to a file in PowerShell Get-Date | Set-Content -Path C:\MyDir\myfile1.txt get_content ## remove the directory along with content Remove-Item -Force -Recurse C:\MyDir\ |
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