Dynamically adjust file in Windows executor

You may have a situation where you need to dynamically change or update a file within the Windows executor. Usually, this would to update a config file. If the file is in XML, the following is an example of how such an update would be made:

      image: windows-server-2019-vs2019:stable
    shell: powershell.exe
      NEW_URL: "https://newurl.org"
      - checkout
      - run:
          name: Update config file
          command: |
            Write-Host("Displaying Original Config");
            type C:\Users\circleci\project\test.config
            [xml]$XmlDocument = Get-Content -Path C:\Users\circleci\project\test.config
            $url = $XmlDocument.SelectSingleNode("configuration/Test/add[@value='https://something.com/']");
            $url.value = "$env:NEW_URL";
            Write-Host("Displaying Updated Config");
            type C:\Users\circleci\project\test.config

The above run command is adjusting the URL present in the following test.config file:


The Write-Host commands are present just to verify the change and can be removed as needed.

Was this article helpful?
1 out of 1 found this helpful