Share this post:This blog post is part of an ongoing series by Adam Gordon. Adam will show you how to use each PowerShell command each week. This week Adam will be covering Add-Content.
When should you use add-content?
The Add-Content cmdlet adds content to a specified file or item. You can specify the content either by entering it in the command, or by specifying an object containing the content.
What version of PowerShell do I use?
Get the PowerShell Version for your machine
$PSVersionTable
This command displays the PowerShell version information for your machine.
How to Use Add-Content
Add a string in all text files that contain an exception
Add-Content –Path .\*.txt *Exclude help* –Value End of file’
The -Path parameter lists all.txt files within the current directory. However, the -Exclude parameter ignores file name that do not match the specified pattern. The -Value parameter specifies a text string that will be written to the files.
Add a date at the end of the specified files
Add-Content -Path .\DateTimeFile1.log, .\DateTimeFile2.log -Value (Get-Date) -PassThru
Get-Content -Path .\DateTimeFile1.log
The Add-Content cmdlet creates new files in the current director. The Get-Date cmdlet outputs the -Value parameter. The -PassThru parameter outputs additional contents to the pipeline.
It is displayed in the PowerShell console because there is no other cmdlet that can receive the output.
The DateTimeFile1.log file is updated by the Get-Content cmdlet.
Add the contents of a file to another file
$From = Get-Content -Path .\ITP.txt
Add-Content -Path .\MSGraph.txt -Value $From
Get-Content -Path .\MSGraph.txt
The Get-Content cmdlet retrieves the contents of ITP.txt, and stores them in the $From variable.
The Add-Content cmdlet updates MSGraph.txt using the contents of $From variable.
The Get-Content cmdlet displays MSGraph.txt.
Pipeline allows you to add the contents of a file to another file.
Get-Content -Path .\ITP.txt | Add-Content -Path .\Adam.txt
Get-Content -Path .\Adam.txt
The Get-Content cmdlet retrieves the contents of ITP.txt. The results are sent to the Add-Content cmdlet which updates Adam.txt. The last Get-Content cmdlet displays Adam.txt.
Make a new file and then copy the content.
Add-Content -Path .\NewFile.txt -Value (Get-Content -Path .\ITP.txt)
Get-Content -Path .\NewFile.txt
The Add-Content cmdlet uses -Path, -Value parameters in order to create a new directory file.
The Get-Content cmdlet retrieves the contents of ITP.txt, an existing file, and passes it to -Value. The Get-Content cmdlet is enclosed in parentheses to ensure that the command ends before the Add-Content command starts.
The Get-Content cmdlet displays contents of the new file, NEWFILE.txt.
Add content to a read only file:
New-Item -Path .\IsReadOnlyTextFile.txt -ItemType File
Set-ItemProperty -Path .\IsReadOnlyTextFile.txt -Name IsReadOnly -Value $True
Get-ChildItem -Path .\IsReadOnlyTextFile.txt
Add-Content -Path .\IsReadOnlyTextFile.txt -Value ‘Add value to read-only text file’ -Force
Get-Content -Path .\IsReadOnlyTextFile.txt
The New-Item cmdlet uses the -Path and -ItemType parameters to create the file IsReadOnlyTextFile.txt in the current directory.
The Set-ItemProperty cmdlet uses -Name, -Value parameters for changing the file’s IsReadOnly to True property.
The Get-ChildItem cmdlet indicates that the file is empty (0), and has the read-only attribute(r).
The -Path parameter is used to specify the file by the Add-Content cmdlet. The -Value parameter contains the text string that will be appended to the file. The -Force parameter adds the text to the read only file.
The Get-Content cmdlet uses a -Path parameter in order to display the contents of the file.
Use the Set-ItemProperty command to remove the read-only attribute. Make sure you set the -Value parameter to False.
Use filters with add-content:
Add-Content -Path C:\PShellTest\* -Filter *.txt -Value “Done”
You can add a filter to Add-Content cmdlet. To indicate the path’s contents, you must include a trailing asterisk (“*”) when using filters to qualify -Path.
Protect-CmsMessage: Last week’s command
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.