Using Set-Content To Modify Your Scripts

1 minute read

Description:

So like many other scripters, I find that I often want my PS files to follow a certain template, so I created a function that will both insert and append predefined text to my scripts so that I can just open VS Code and modify the “body”. Here goes:

To Resolve:

  1. Use the following:
# Edit these extensions to the type of files you want to add content to.
$Include = @("*.txt", "*.ps1", "*.log")

# $Source = Get-Childitem "C:\Test" -Include "$Include" -Recurse
$Source = Get-Childitem "C:\Test" -Include "$Include"

Foreach ($File In $Source)
{

Log "Processing $File ..." 

# Remember to use the escape character "`" before every dollar sign and ` character. For example `$myVar and ``r``n (new line)
$Preformatting = @"
Multi-Line
Text
To
Insert
At
Top
"@

$CurrentFile = Get-Content $File

$PostFormatting = @"
Multi-Line
Text
To
Insert
At
Bottom
"@

$Val = -Join $Preformatting, $CurrentFile, $PostFormatting
Set-Content -Path $File -Value $Val
Log "$File rewritten successfully"
}

NOTE: At first I couldn’t get this to work unless I moved the files to a new directory, but after a couple hours of research, I can say this does work. It will keep the original files in their current folders.

This can be found in my gwFileSystem section.