Windows PowerShell


 

PowerShell Script Files - Add-content

PowerShell Script Files - Add-content

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 Add-content

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.

cls
# PowerShell script which append text to a memo
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.

1) get-help add-content
    (help ac) If you like abbreviations.
    (help ac -full) If you prefer examples.

Example 2 Add-content, complete with two -parameters

cls
# PowerShell script using parameters to control text added to a file
add-Content -path "D:\PowerShell\Memo.txt" -value "Remember Jo's birthday"

Learning Points

Note 1:  I would like you to compare and contrast Example 2 with Example 1.  My purpose is to show you that in you add values in the default positions, then you do not need to explicitly add the parameters.  In this instance PowerShell can deduce what to do with the data for -path and -value, from the position of the text strings.

  ˚

The Content Family of Cmdlets

You may have noticed from, 'help ac -full', that PowerShell supports a family of verbs that act upon the noun 'Content'.

Add-Content
Clear-Content
Get-Content
Set-Content

There is also Out-File, a very useful command to save results to disk, rather than write to screen.

See Also PowerShell Tutorials:

PowerShell Home  • Com  • Shell Application  • Active Directory  • QAD Snap-in  • Get-Member

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.