Script works fine from admin Powershell prompt but not when using sudo from non-admin prompt


NickJP

Well-known member
Member
Local time
10:36 PM
Posts
36
OS
Windows 11 Pro
I have a couple of Powershell scripts for changing my DNS servers - one for changing the DNS setting to static servers, the other for changing back to DHCP. The scripts run OKfrom Powershell when right-clicking the PS icon and selecting "Run as administrator", and then executing either script succeeds, but if I have a non-administrator PS windows open, and try to run either script via sudo:

"sudo <scriptname>.ps1"

I get this error from either script:

Code:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS D:\Users\Nick\scripts> sudo .\dnsdhcp.ps1
Unknown error: %1 is not a valid Win32 application. (0x800700C1)
PS D:\Users\Nick\scripts>

The script itself is really simple:
Code:
netsh interface ip set dns "Ethernet" dhcp
ipconfig /flushdns
ipconfig /all | findstr /R "DNS\ Servers"

Any suggestion on a possible fix?
 
Windows Build/Version
25H2 build 26200.9457

My Computer My Computer

At a glance

Windows 11 ProIntel i5-12600K128GbnVidia Quadro P1000
OS
Windows 11 Pro
Computer type
PC/Desktop
Manufacturer/Model
Gigabyte
CPU
Intel i5-12600K
Motherboard
Z690 Aorus Pro
Memory
128Gb
Graphics Card(s)
nVidia Quadro P1000
Sound Card
N/A
Monitor(s) Displays
BenQ PD3205U, Eizo EV3237
Screen Resolution
3840x2160, 3840x2160
Hard Drives
Samsung SSD 980 Pro
Samsung SSD 970 EVO Plus
Sabrent Rocket Q
Case
Be Quiet
What about

sudo powershell.exe -File "C:\Path\To\script.ps1" (or pwsh.exe, whatever you're using)
 

My Computer My Computer

At a glance

Windows 11 Enterprise 25H2
OS
Windows 11 Enterprise 25H2
Thanks. That works.
 

My Computer My Computer

At a glance

Windows 11 ProIntel i5-12600K128GbnVidia Quadro P1000
OS
Windows 11 Pro
Computer type
PC/Desktop
Manufacturer/Model
Gigabyte
CPU
Intel i5-12600K
Motherboard
Z690 Aorus Pro
Memory
128Gb
Graphics Card(s)
nVidia Quadro P1000
Sound Card
N/A
Monitor(s) Displays
BenQ PD3205U, Eizo EV3237
Screen Resolution
3840x2160, 3840x2160
Hard Drives
Samsung SSD 980 Pro
Samsung SSD 970 EVO Plus
Sabrent Rocket Q
Case
Be Quiet
Back
Top Bottom