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.
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.
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
(help ac) If you like abbreviations. (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!
# 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 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 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).
Guy Recommends: SolarWinds Engineer's Toolset v10
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 Clear-Host
Get-Command -Noun Content
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
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.
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.