PowerShell Script Files – Add-Content Cmdlet
Few built-in cmdlets express their purpose as clearly as Add-Content. In fact the only danger with this command is ‘over-think’, just remember that PowerShell takes care of opening and closing files automatically. Consequently, there is no need to waste time looking for non-existent file-open, or file-save commands. If the file specified by Add-Content does not exist, then PowerShell even creates it for you.
Topics for PowerShell Add-Content
- Example 1: Add-Content, Short and Sweet
- Example 2: Add-Content, Complete with Two -Parameters
- Example 3: Adding a Date
- The Content Family of Cmdlets
♣
Example 1: Add-Content, Short and Sweet
Here is a PowerShell example that adds the string "Remember Mam’s birthday" to a file called Memo.txt. I strongly recommend that you change the path from C: \Memo.txt to a folder such as C: \PowerShell\Memo.txt. If you need any help in executing the code, then see here.
# PowerShell Add-Content Example Script
Clear-Host
Add-Content "C:\Memo.txt" "Remember Mam’s birthday"
Learning Points
Note 1: Observe the classic Verb-Noun pairing add (verb) content (noun). Furthermore, the whole family use the singular word ‘content’.
Call for Help
When you experiment with a new PowerShell command, it benefits from being probed with Get-Help. As you study help and its examples, note how the position is important, For example: -Path must be the first element, and -Value in position two.
# PowerShell find parameters for Add-Content
Clear-Host
Get-Help Add-Content
Note 1: You could try "help ac" If you like abbreviations, or help ac -full, if you prefer examples.
Recommended: Solarwinds’ Permissions Analyzer – Free Active Directory Tool
I like the Permissions Monitor because it enables me to see WHO has permissions to do WHAT at a glance. When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, and takes into account network share access, then displays the results in a nifty desktop dashboard!
Think of all the frustration that this free SolarWinds utility saves when you are troubleshooting authorization problems for user’s access to a resource. Give this permissions monitor a try – it’s free!
Download SolarWinds’ Free Permissions Analyser – Active Directory Tool
Example 2: Add-Content, Complete with Two -Parameters
# PowerShell script using parameters to control text
Clear-Host
$File = "D:\PowerShell\Memo.txt"
Add-Content -Path $File -Value "Remember Jo’s birthday"
Learning Points
Note 2: I would like you to compare and contrast Example 2 with Example 1. My point is that if you add values in the default positions, then you do not need to explicitly add the parameters. In this instance PowerShell could deduce what to do with the data for -path and -value, from the position of the text strings.
Note 3: One reason that I used the variable $File is that I want to remind to change this value for the path before you run this script on your machine.
Example 3: Adding a Date
Our task, to append today's date to a text string. Our assumption is that today is Mike's birthday.
Solution:
# PowerShell Add-Content in Expression mode
Clear-Host
$File = "D:\PowerShell\Memo.txt"
$Birthday = Get-Date -UFormat " %B %d"
Add-Content -Path $File -Value "Mike's birthday $Birthday"
Invoke-Item $File
Note 4: The line below did not produce the desired result.
Add-Content -Path $File -Value "Mike's birthday Get-Date"
Note 5: One work-around is to put PowerShell in expression mode with:
Add-Content -Path $File -Value "Mike's birthday $(Get-Date)"
Another work around was to introduce the $Birthday variable as in Example 3 above
Note 6: I appended 'Invoke-Item' which opens the Windows explorer at the file location.
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM) v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
Research PowerShell Alias ‘Ac’
# PowerShell Alias Ac
Get-Alias -Definition Add-Content
With Microsoft, there are always at least three ways of doing everything, what seems like redundancy when you are an expert, seems like perspective when you are a beginner. One obvious example is that you can abbreviate Format-Table to ft. As you increase your range of PowerShell commands, keep an eye out for another PowerShell Alias, for example gci (Get-Childitem).
Researching Similar PowerShell Content Cmdlets
You can use Get-Command to research more of PowerShell's 'Content' family. All you need to do is append the -Noun parameter. Incidentally, you could also try -Verb Add.
# PowerShell Content Cmdlet Research
Clear-Host
Get-Command -Noun Content
Results:
————–
Add-Content
Clear-Content
Get-Content
Set-Content
Summary of Add-Content Cmdlet
Few PowerShell cmdlets express their purpose as clearly as Add-Content. If the file specified by Add-Content does not exist, then PowerShell even creates it for you.
If you like this page then please share it with your friends
See more Microsoft PowerShell file tutorials:
• PowerShell Home • Add-Content • Get-Content • Set-Content • PowerShell -Filter • Test-Path
• PowerShell Get-ChildItem • Get-ChildItem -Include • Get-ChildItem -Exclude • Compare-Object
• PowerShell Registry • Get-Credential • PowerShell ItemProperty • PowerShell ItemPropery GCI
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.