Bash Script Converting to PowerShell to edit "wp-config", for all users RRS feed

  • Question

  • Hi 
    I need to convert an existing Bash Script into PowerShell based script.

    The Bash Script edits the line # 45 of wp-config.php file for each user in /home/ ; by traversing to location /var/www/html/ for each user. the script is as follows:

    BASH Script: for user in userlist; do cd /home/$user/www; sed -i "45idefine( 'WP_AUTO_UPDATE_CORE', false );" wp-config.php;

    I need to perform same task in Windows Based Server, using PowerShell Script. I am newbie to PowerShell. I researched some cmdlets and wrote the following script:

    PowerShell Script: 

    Get-ChildItem -Path /home/  -Recurse -Force -Include wp-config.php | ForEach-Object
    $content = Get-Content 
    $content[45] = -replace "45","'WP_AUTO_UPDATE_CORE', false" 
    $content | Set-Content 

    But it didn't work. aslo I realized that I have given incorrect path in the above mentioned PowerShell Script. 

    Can you guys please help me in improving it, to do the required task or guide me how may I learn it ASAP to do it within time deadline!




    Tuesday, December 11, 2018 4:27 PM

All replies

  • Welcome to the Microsoft Powershell forum.

    Please edit your post and format your code as code here in the forum. Use the code posting tool provided in the edit bar of the post editor.  Thanks.

    But it didn't work.

    What exactly does that mean? Did you get errors? Didn't it do what you expected? What did you expect then? If you get an error - post it here. (as code formatted as well please)

    ...aslo I realized that I have given incorrect path in the above mentioned PowerShell Script.

    so change it to the correct path.

    Live long and prosper!


    Tuesday, December 11, 2018 6:16 PM
  • $content[45] = $content[45] -replace 'WP_AUTO_UPDATE_CORE','some new value'

     I suggest taking the time to learn basic PowerShell.  There are many pieces of your scritp that are syntactically wrong

    help Get-Content -online
    help Set-Content -online


    Tuesday, December 11, 2018 6:32 PM
  • Wednesday, December 12, 2018 6:55 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Just do it.

    Monday, December 24, 2018 3:20 AM