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
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
Note 1: Observe the classic verb-Noun pairing add (verb) content (noun). Furthermore, the whole family use the singular word 'content'.
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.
1) Get-Help Add-Content
# PowerShell script using parameters to control text
Note 1: 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 2: 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.
Research PowerShell Alias 'Ac'
# PowerShell Alias Ac
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).
This Engineer's Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems. Guy says it helps me monitor what's occurring on the network, and each tool teaches me more about how the underlying system operates.
There are so many good gadgets; it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools. Try the SolarWinds Engineer's Toolset now!
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
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:
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.