0

EDIT: Thank you everyone for the help! The answer was to literally delete the "-" in the script and retype. I copied Set-DNSClientServerAddress "Ethernet" -ServerAddresses ("1.1.1.1","8.8.8.8") from a different stackoverflow answer, and apparantly the dash was the wrong kind of dash.

can anyone help me figure out why this simple script is giving an error, when running the same exact command by copy and pasting it into an existing powershell window works fine?

$confirmation = Read-Host "Change DNS?"
if ($confirmation -eq 'y') 
{
    Set-DNSClientServerAddress "Ethernet" –ServerAddresses ("1.1.1.1","8.8.8.8")
}
pause

When that is ran manually by copy and pasting into Powershell, it works perfectly.

However, when saving that code into a test.ps1 file, and then right clicking on the file to Run with Powershell, it gives this error:

Error

5
  • 1
    Looks like an encoding issue. In the error message, there are some odd characters (including a rogue quote) in place of the - in -ServerAddresses. Commented Nov 19, 2021 at 19:42
  • 1
    I agree, best approach is probably to save the script file as UTF8 With BOM. If you are using VSCode, hit the command pallet (ctrl+shift+p), type encoding and select Change file encoding. Select UTF-8 With BOM. Commented Nov 19, 2021 at 19:46
  • 1
    The dash is unicode 'EN DASH' (U+2013). Powershell 5 can't recognize utf8 no bom. Commented Nov 19, 2021 at 19:53
  • To summarize: Your script contains a (EN DASH, U+2013) character instead of the usual ASCII-range - (HYPHEN-MINUS, U+002D) before ServerAddresses. That in itself is not a problem, but because your UTF-8-encoded script file lacks a BOM, it is misinterpreted by Windows PowerShell, which sees the as â€. The solution is to save your script file as UTF-8 with BOM; see this answer to the first linked duplicate. Commented Nov 19, 2021 at 20:06
  • Thank you everyone for the help! The answer was to literally delete the "-" in the script and retype. I copied Set-DNSClientServerAddress "Ethernet" -ServerAddresses ("1.1.1.1","8.8.8.8") from a different stackoverflow answer, and apparently the dash was the wrong kind of dash. I just deleted the dash and pressed - again, and it worked! Commented Nov 20, 2021 at 13:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.