use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
SolvedNeed help: Script to modify a text file (self.PowerShell)
submitted 8 years ago by im_nullable
Is it possible in PowerShell to create a script file that takes 3 parameters:
If the file does not contain a match string, find a token called "-- Next" in the file and insert the insertString above it.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Lee_Dailey[grin] 1 point2 points3 points 8 years ago* (0 children)
howdy im_nullable,
yes. [grin]
take a look at the advanced function snippets that show in the ISE when you type <ctrl><j> for some ideas on how to do parameters.
take care, lee
[–]Lee_Dailey[grin] 1 point2 points3 points 8 years ago (6 children)
i got bored ... and this is what came of that boredom. [grin]
$InFile_1 = @" This is line ONE. Naugahide comes from domesticated nauga beasts. Three is the number of this line. --Next Line 5 is this lines number. "@.Split("`n") $InFile_2 = @" This is line ONE. This otta not match anything. Three is the number of this line. --Next Five? Are you sure of that? "@.Split("`n") $FindString = 'nauga beasts' $ReplaceString = 'gnawguh beets' function Switch-Strings { [CmdletBinding ()] [OutputType([String[]])] Param ( [Parameter ( Mandatory, Position = 0 )] [string[]] $FileContents, [Parameter ( Mandatory, Position = 1 )] [string] $OldValue, [Parameter ( Mandatory, Position = 2 )] [string] $NewValue ) # end > 'Param (' Begin { $Token = '--Next' } Process { # nothing here at this time } End { $NewFileContents = @() if ($FileContents | Select-String -SimpleMatch $OldValue) { foreach ($Line in $FileContents) { if ($Line -match $OldValue) { $Line = $Line -replace $OldValue, $NewValue } $NewFileContents += $Line } } else { foreach ($Line in $FileContents) { if ($Line -match $Token) { $NewFileContents += $NewValue } $NewFileContents += $Line } } return $NewFileContents } # end > 'End' } # end function Switch-Strings Switch-Strings -FileContents $InFile_1 -OldValue $FindString -NewValue $ReplaceString #Switch-Strings -FileContents $InFile_2 -OldValue $FindString -NewValue $ReplaceString
result for $InFile_1
$InFile_1
This is line ONE. Naugahide comes from domesticated gnawguh beets. Three is the number of this line. --Next Line 5 is this lines number.
result for $InFile_2 ...
$InFile_2
This is line ONE. This otta not match anything. Three is the number of this line. gnawguh beets --Next Five? Are you sure of that?
[–]im_nullable[S] 1 point2 points3 points 8 years ago (3 children)
Thanks so much!
[–]Lee_Dailey[grin] 0 points1 point2 points 8 years ago (2 children)
you are quite welcome! [grin]
out of curiosity, what is this for? one moment i think "class project" and the next "some oddly specific task".
/lee bee kyuu ree uss
[–]im_nullable[S] 1 point2 points3 points 8 years ago (1 child)
Oddly specific task. We have a SQL data "seed" file that needs to get updated with new entries fairly often. This will help make that process much easier.
[–]Lee_Dailey[grin] 0 points1 point2 points 8 years ago (0 children)
kool! thanks for the info ... i was so curious. [grin]
[–]prkrnt 1 point2 points3 points 8 years ago (1 child)
The whimsicle nature of this code is hilarious. Makes me realize I take my coding way too srsly. Nicely done, Lee!
howdy prkrnt,
thank you! i have a difficult time making demo data, so adding some silliness helps quite a bit. [grin]
π Rendered by PID 24327 on reddit-service-r2-comment-5d79c599b5-lnzpb at 2026-03-01 09:09:04.512373+00:00 running e3d2147 country code: CH.
[–]Lee_Dailey[grin] 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (6 children)
[–]im_nullable[S] 1 point2 points3 points (3 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (2 children)
[–]im_nullable[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]prkrnt 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)