Need help with powershell script


I am trying to do a search and replace of a complete line in a text file, but it does not work.


Parse a text file, and if a match is found, one a line, then replace entire line with new text


Text File contents:

MinimumPasswordAge = 10
MaximumPasswordAge = 999
MinimumPasswordLength = 888
PasswordComplexity = 777
PasswordHistorySize = 222


$Filepath= "c:\test"
$name = hostname
$cbefore = $Filepath + "\before-" + $name + ".inf"
$cafter = $FilePath + "\after-" + $name + ".inf"
$cpolicy = Get-content $cbefore

$cpolicy -replace '^MinimumPasswordAge.$', 'MinimumPasswordAge = 9'
$cpolicy -replace'^MaximumPasswordAge.$', ("MaximumPasswordAge = 9")
$cpolicy -replace '^MinimumPasswordLength.$',("MinimumPasswordLength = 9")
$cpolicy -replace '^PasswordComplexity.$',("PasswordComplexity = 9")
$cpolicy -replace '^PasswordHistorySize.$',("PasswordHistorySize = 9")

$cpolicy | Set-content $cafter

The data is not properly written afterwards, nor does the variable cpolicy have the update text

Desired results:

MinimumPasswordAge = 9
MaximumPasswordAge = 9
MinimumPasswordLength = 9
PasswordComplexity = 9
PasswordHistorySize = 9
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You are looking for A string at the beginning of a line, followed by a single character, then end of line. Of course that does not work ;-). For example:
$cpolicy -replace '^MinimumPasswordAge =.*$', 'MinimumPasswordAge = 9' 

Open in new window

mbudmanAuthor Commented:
So how do i fix it?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
My example is the correct replacement for the first setting.
Maybe a bit overengineered, but flexible; you can just change the $Replace hashtable as desired:
$Filepath= "C:\test"
$Name = hostname
$InFile = "$($Filepath)\before-$($Name).inf"
$OutFile = "$($Filepath)\after-$($Name).inf"
$Replace = @{
	'MinimumPasswordAge' = 9
	'MaximumPasswordAge' = 9
	'MinimumPasswordLength' = 9
	'PasswordComplexity' = 9
	'PasswordHistorySize' = 9

Get-Content $Infile | ForEach-Object {
	$ReplacedLine = $Null
	ForEach ($Key In $Replace.Keys) {
		If ($_ -match "\A$($Key)\s*=\s*\d+\s*\Z") {
			$ReplacedLine = "$($Key) = $($Replace[$Key])"
	If ($ReplacedLine) {$ReplacedLine} Else {$_}
} | Set-Content -Path $OutFile

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.