stack twitter tryhackme rss linkedin cross

Wilco van Esch

Skip to main content

Search results

    Edit a file on startup in Windows

    Example use case

    Maybe you need a value in a Docker config.json to be updated and persist, but the value resets whenever Docker is closed down.

    Solution

    Create a startup script

    In any text editor or IDE, create a startup.ps1 file.

    In the file, add the following (while replacing with the values relevant to you):

    powershell -Command "(gc \\wsl$\Ubuntu-20.04\home\wilco\.docker\config.json) --replace 'oldvalue', 'newvalue' | sc \\wsl$\Ubuntu-20.04\home\wilco\.docker\config.json"

    gc: Get-Content (get the contents of a file)
    sc: Set-Content (write to a file)

    Place this file in a convenient location, such as your user directory or C:\tools.

    Schedule a startup task

    1. Open the Task Scheduler (Windows key + start typing task, then select it)
    2. Hit Create Basic Task.. (under Task Scheduler Library)
    3. Give the task a name you will recognise later
    4. Hit Next
    5. Choose When I log on
    6. Hit Next
    7. Ensure Start a program is selected
    8. Hit Next
    9. For Program/script, enter PowerShell.exe
    10. For Add arguments, add -ExecutionPolicy Bypass C:\YOURSCRIPTLOCATION\startup.ps1
    11. Hit Next
    12. Hit Finish

    -ExecutionPolicy Bypass: you're allowing PowerShell to do anything in this script, nothing is blocked or warned about.

    And there you go. Check the file has the old value, reboot, and you will find the file is now updated.