The issue is that you can't reference the changed value in the same task.
I created a new Powershell task and just called Write-Host "env:IP environment variable set to ($env:IP)" which output the IP address correctly.
Pipeline Variables are handed to PowerShell as environment variables, which means that $ip exists only with the script while you want to use $env:ip to pick the environment variable value.
Note Secret variables are not passed automatically: you must add an explicit env: in YAML or as arguments in Classic Editor. The latter means that the PowerShell code must ...