none
Need to edit ini file entry for roughly 4000 files within a path\subfolder RRS feed

  • Question

  • Hi, am new to PowerShell and I need some help with a PowerShell script I have tried to make that needs to update an entry [postcode=] in an .ini file within a tree of subfolders.

    I have a share S:\sharename which contain terminal server profile folders that contain the .ini files amongst other files, like this;

    S:\sharename\z1, S:\sharename\z2, S:\sharename\z3, S:\sharename\z4, S:\sharename\z5 and so on.

    I have tried the below, but this only updates the postcode.ini file located at the top level, S:\Sharename not S:\Sharename\z1, S:\Sharename\z2 etc.

    $afd = "afd"
    $FilePath = "S:\Sharename"
    (Get-Content ($FilePath + "\postcode.ini")) | Foreach-Object {$_ -replace '^postcode=.+$', ("postcode=" + $afd)} | Set-Content  ($Filepath + "\postcode.ini")

    I cannot seem to get the "-recurse" option to be recognised anywhere within the script

    Any help would be greatly appreciated

    Thanks, Greg

    Tuesday, March 15, 2016 12:09 PM

Answers

  • Your almost there :) Like Mike said, use 'get-childitem'. I typed the following out of my head, if it works fine, if it doesn't correct the code yourself.

    $files = Get-ChildItem $FilePath -Filter postcode.ini -Recurse  
    
    $files | % { 
    
    $FileFullName = $_.FullName
    $content = get-content $FileFullName
    $newcontent = $content -replace '^postcode=.+$', ("postcode=" + $afd)
    $newcontent | set-content $FileFullName
    
    }
    Tip: To test the script use '$files[0] | % {' and run the foreach per file to see how it runs and how the variables are populated.


    Cheers,

    Ruud
    Twitter:    Blog: www.ruudborst.nl  LinkedIn:    
    Note: Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.



    • Edited by Ruud BorstMVP Tuesday, March 15, 2016 2:29 PM
    • Marked as answer by Groogn Tuesday, March 15, 2016 3:11 PM
    Tuesday, March 15, 2016 2:27 PM

All replies

  • Hi,

    Use Get-ChildItem to get your list of files and pipe the results into Get-Content.

    http://ss64.com/ps/get-childitem.html


    • Proposed as answer by Ruud BorstMVP Tuesday, March 15, 2016 2:27 PM
    Tuesday, March 15, 2016 12:14 PM
  • @MikeLaughlin

    Thanks for the link,

    I have managed to get the info from the files, but am struggling with the correct syntax for Set-Content in order to now set the retrieved value of postcode=c:\devlive\qaddress.ini  to postcode=afd

    $FilePath = "C:\silverlink"
    $Filename = "icsliv95.ini"
    Get-ChildItem -Path $FilePath -Include $Filename -Recurse | Get-Content | Select-String "postcode=C:*" 


    PS C:\Silverlink> $FilePath = "C:\silverlink"
    $Filename = "icsliv95.ini"
    Get-ChildItem -Path $FilePath -Include $Filename -Recurse | Get-Content | Select-String "postcode=C:*"
    
    postcode=c:\devlive\qaddress.ini
    postcode=c:\devlive\qaddress.ini
    postcode=c:\devlive\qaddress.ini
    postcode=c:\devlive\qaddress.ini
    postcode=c:\devlive\qaddress.ini
    postcode=c:\devlive\qaddress.ini
    postcode=c:\devlive\qaddress.ini
    postcode=c:\devlive\qaddress.ini
    postcode=c:\devlive\qaddress.ini
    postcode=c:\devlive\qaddress.ini
    postcode=c:\appslink\qaddress.ini

    Thanks again

    Greg

    Tuesday, March 15, 2016 2:21 PM
  • Replace-FileString.ps1 script might be helpful:

    Replacing Strings in Files Using PowerShell


    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 15, 2016 2:26 PM
    Moderator
  • Your almost there :) Like Mike said, use 'get-childitem'. I typed the following out of my head, if it works fine, if it doesn't correct the code yourself.

    $files = Get-ChildItem $FilePath -Filter postcode.ini -Recurse  
    
    $files | % { 
    
    $FileFullName = $_.FullName
    $content = get-content $FileFullName
    $newcontent = $content -replace '^postcode=.+$', ("postcode=" + $afd)
    $newcontent | set-content $FileFullName
    
    }
    Tip: To test the script use '$files[0] | % {' and run the foreach per file to see how it runs and how the variables are populated.


    Cheers,

    Ruud
    Twitter:    Blog: www.ruudborst.nl  LinkedIn:    
    Note: Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.



    • Edited by Ruud BorstMVP Tuesday, March 15, 2016 2:29 PM
    • Marked as answer by Groogn Tuesday, March 15, 2016 3:11 PM
    Tuesday, March 15, 2016 2:27 PM
  • @MikeLaughlin

    Thanks for the link,

    I have managed to get the info from the files, but am struggling with the correct syntax for Set-Content in order to now set the retrieved value of postcode=c:\devlive\qaddress.ini  to postcode=afd

    <snip>

    Here's an example of one method you could use:

    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-15-2016> cat .\example.txt
    [state=az]
    [postcode=]
    [city=mesa]
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-15-2016> (Get-Content .\example.txt).Replace('[postcode=]','[postcode=afd]') | Set-Content .\example.txt
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\3-15-2016> cat .\example.txt
    [state=az]
    [postcode=afd]
    [city=mesa]


    Tuesday, March 15, 2016 2:28 PM
  • @RuudBorst - You sir are a genius!! Worked first time!!

    I cannot thank you and @MikeLaughlin enough for your time and expertise

    Thanks Greg

    Tuesday, March 15, 2016 3:13 PM
  • Cheers Greg, you're very welcome. Glad we could help out.

    Tuesday, March 15, 2016 4:14 PM