Guy recommends :
Free Permissions
Analyzer Tool

Solarwinds Free Download of Permissions Analyzer

 

View the effective permissions for a folder or shared drive. Free download try it now!


PowerShell Set-Content

PowerShell Script Files - Set-Content Cmdlet

I don't use Set-Content very often, this is because for routine tasks that need to save to file I prefer to use Out-File.  The one job that I have for the Set-Content cmdlet is editing a file with -Replace.  What makes me wary of Set-Content is that instead of appending, its default behaviour is to delete the contents of a file.

 ♣

Example 1: Set-Content Basics

# PowerShell Set-Content cmdlet
Clear-Host
$File = "E:\Memos\Accounts.txt"
Set-Content -path $File  -value "Rent 500"

Learning Points

Note 1:  The reason that I used the variable $File to define the path, is that I want to remind you to change its value before running this script on your machine.

Note 2: If the file specified does not exist PowerShell creates it!

Note 3: However, if the file has existing content, the new value will overwrite the old entries with the -value.

Note 4:  One reason that PowerShell is replacing VBScript is the ease with which PowerShell creates, opens and closes files.  In fact it handles these basic file operations automatically, thus don't look for, or worry about, 'File Close', PowerShell will handle the operation natively.

Example 2: Set-Content -Replace

Although -Replace does not appear amongst the parameter's of any PowerShell cmdlet it clearly works in this context.  Observe in this example how the two Content cmdlets work together to edit the text.

# Example of Set-Content -Replace
Clear-Host
$File = "E:\Memos\Accounts.txt"
foreach ($str in $File)
{
$content = Get-Content -path $str
$content | foreach {$_ -Replace "Rent 500", "Rent 525"} | Set-Content $str
}
Write-Host "After replace `n"
Get-Content $File

Note 5: See more about -Replace

Research Set-Content Parameters

# PowerShell Set-Content Parameters
Clear-Host
Get-Help Set-Content -full

Checking the help file will reveal useful parameters, for instance you can employ the -Force parameter to overwrite read-only files.  Reading the help file also reveals that this command is primarily used for writing text to multiple files, perhaps this is why I am wary of using it myself and avoid recommending this particular cmdlet for others to practice with.

Researching Similar PowerShell Content Cmdlets

# PowerShell Content Cmdlet Research
Clear-Host
Get-Command -Noun Content

Results show:

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

Also Recommended Out-File

Research PowerShell Alias 'Sc'

# PowerShell Alias Ac
Get-Alias -definition Set-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).

Summary of PowerShell's Set-Content Cmdlet

In VBScript dealing with files was never straightforward.  However, with PowerShell it's so effortless that you may not realize that the Set-Content cmdlet opens and closes as part of its job description.

Set-Content is a simple enough cmdlet to understand.  What it does is write text to files.  Just playing with Set-Content leads to questions such as 'Why bother', or 'What's the point?'  I hope that your answer to such questions will give you a PowerShell technique that you can incorporate in bigger more complex scripts.

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.

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.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2013 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: