PowerShell Clear-Content Cmdlet
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Set-Content cmdlet in PowerShell.
https://cloudaffaire.com/powershell-set-content-cmdlet/
In this blog post, we will discuss Clear-Content cmdlet in PowerShell. You can use Clear-Content cmdlet to delete the contents of a file. Clear-Content cmdlet removes the existing contents of the file, but the file itself is not deleted. To delete the file along with its content use, Remove-Item cmdlet.
Clear-Content Cmdlet Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 |
## Clear-Content ## [-Path] ## [-Filter ## [-Include ## [-Exclude ## [-Force] ## [-Credential ## [-WhatIf] ## [-Confirm] ## [-UseTransaction] ## [-Stream ## [ |
Clear-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.
- –Exclude: Specifies, as a string array, strings that this cmdlet omits from the path to the content. The value of this parameter qualifies the Path parameter.
- –Filter: Specifies a filter in the provider’s format or language. The value of this parameter qualifies the Path parameter.
- –Force: Forces the command to run without asking for user confirmation.
- -Include: Specifies, as a string array, content that this cmdlet clears. The value of this parameter qualifies the Path parameter.
- –LiteralPath: Specifies the paths to the items from which content is deleted. Unlike the Path parameter, the value of LiteralPath is used exactly as it is typed.
- –Path: Specifies the paths to the items from which content is deleted. Wildcards 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.
- –WhatIf: Shows what would happen if the cmdlet runs. The cmdlet is not run.
PowerShell Clear-Content Cmdlet:
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
######################################### ## PowerShell | Cmdlet | Clear-Content ## ######################################### ## PowerShell Latest Version (5) ## create a directory and some files with content and a function to read content of multiple files New-Item -ItemType Directory -Force -Path C:\MyDir\ 1..5 | ForEach-Object { New-Item -ItemType file -path "C:\MyDir\myfile$_.txt" } 1..5 | ForEach-Object { Add-Content -Path C:\MyDir\myfile$_.txt -Value "$_" } Set-ItemProperty -Path C:\MyDir\myfile5.txt -Name IsReadOnly -Value $True function get_content() { Get-ChildItem 'C:\MyDir' | ForEach-Object { @' ..........{0}.......... {1} '@ -f $_.Name, (Get-Content $_.FullName -Raw) } } ## get contents of all files get_content ## delete content of a file in PowerShell Clear-Content -Path C:\MyDir\myfile1.txt get_content ## delete contents of multiple files at once in PowerShell Clear-Content -Path C:\MyDir\*.txt -Exclude myfile5.txt get_content ## delete content of a readonly file in PowerShell Clear-Content -Path C:\MyDir\myfile5.txt -Force 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 New-Item cmdlet in PowerShell.
To get more details on PowerShell, kindly follow below official documentation