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

haliofin
  • 11
  • 4
  • 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`. – boxdog Nov 19 '21 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. – Steven Nov 19 '21 at 19:46
  • 1
    The dash is unicode 'EN DASH' (U+2013). Powershell 5 can't recognize utf8 no bom. – js2010 Nov 19 '21 at 19:53
  • To summarize: Your script contains a `–` (EN DASH, [`U+2013`](http://www.fileformat.info/info/unicode/char/2013)) character instead of the usual ASCII-range `-` (HYPHEN-MINUS, [`U+002D`](http://www.fileformat.info/info/unicode/char/2d)) 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](https://stackoverflow.com/a/49665637/45375) to the first linked duplicate. – mklement0 Nov 19 '21 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! – haliofin Nov 20 '21 at 13:43

0 Answers0