Changing a parameter in a configuration file via PowerShell.

What am I doing wrong here? I just need to replace the "shared_buffers = 32MB value to 256MB in a configuration file(s)?

(Get-Content F:\hp\UCMDB\DataFlowProbe\pgsql\data\postgresql.conf)
|ForEach-Object {$_ -replace “shared_buffers = 32MB”, “shared_buffers = 256MB”}
|Set-Content F:\hp\UCMDB\DataFlowProbe\pgsql\data\postgresql.conf

Thank you for any help you could offer?

That code works. When you remove the final pipe and Set-Content cmdlet, does it write the correct data to the screen? My first thought is that you’re working with a read-only file; however, that would probably indicate an error. Maybe not, so try the Set-Content cmdlet with the -Force parameter. Speaking of errors, are you seeing any errors; you didn’t indicate that one way, or another.

I have removed the | and the script seems to execute but the change never seems to take place. I am getting a mess at the end of the script echo:

cmdlet set-content at command pipeline position 1
Supply values for the following parameters:

it seems if i make an entry - the entire file go to blank.


The “Value” error at the end is indicating that nothing is being supplied to the Set-Content cmdlet’s mandatory -Value parameter. Type Set-Content by itself into the PowerShell console and you’ll see what I mean. In fact, you’ll see this with any cmdlet, or function, that requires a specific parameter(s) have a value when it’s run.

I have this working with an example file that (at times), both did, and didn’t contain ‘shared_buffers = 32MB’ and it worked. I’m not sure what else to say. Can you post the beginning to end run, so we can see everything? Providing my image shows below, you should be able to see this worked for me.

Split your code by Pipe, but LEAVE the pipe at the end of line!

(Get-Content F:\hp\UCMDB\DataFlowProbe\pgsql\data\postgresql.conf) |
ForEach-Object {$_ -replace “shared_buffers = 32MB”, “shared_buffers = 256MB”} |
Set-Content F:\hp\UCMDB\DataFlowProbe\pgsql\data\postgresql.conf

The pipe at the END of the line did the trick and the code is working as desired. I can’t say thanks enough.