A powershell script to update Windows hosts file with current WSL ip address(es)
this Windows powershell file is designed to update Windows hosts file with current wsl ip address allowing integration with wsl provided services. It can also launch specific commands to be run on the wsl host.
Marco Guardigli [email protected] mar 20, 2022
updated on may 12 2022, to support ubuntu 20.04, and multiple ip on WSL machine, after Klaudija Stefan ([email protected]) comment.
-
Place this hostedit.ps1 file in c:\windows\system32\drivers\etc
-
configure it accordingly to your names editing $wsl_name and $names variables
-
add command chaining features, placing commands to be launched on wsl host in c:\windows\system32\drivers\etc\cmdchain.txt
-
execute it when needed from a powershell admin prompt (it will not work if not admin)
-
if you get an error saying that powershell script execution is not enabled, issue the following command from a powershell prompt
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine (ref https://stackoverflow.com/questions/54776324/powershell-bug-execution-of-scripts-is-disabled-on-this-system )
- Create a desktop shortcut for powershell.exe and name it "hostedit"
- Edit its properties and add c:\windows\system32\drivers\etc\hostedit_v02.ps1 at the end of the target line leaving a space after "powershell.exe"
- When needed, right click on the shortcut and run as administrator
For Windows11:
- Install the last version of Powershell (tested with 7)
- Go to "C:\Program Files\PowerShell\7" and Create a desktop shortcut for pwsh.exe and name it "hostedit"
- Edit its properties and add c:\windows\system32\drivers\etc\hostedit_v02.ps1 at the end of the target line leaving a space after "C:\Program Files\PowerShell\7\pwsh.exe"
- When needed, right click on the shortcut and run as administrator