Kitten
Kitten

Reputation: 1

powershell spawned by .vbs file does not recognize script files... (Windows 11)

I'm simply trying to run a .ps1 from a .vbs (did this before on w10)

‪C:\Scripts\Test.vbs

command = "powershell.exe -ExecutionPolicy Bypass -nologo -command ‪C:\Scripts\Test.ps1"
set shell = CreateObject("WScript.Shell")
shell.Run command

Error: (when opening .vbs) (uses windows based script host)

‪C:\Scripts\Test.ps1 : The term '‪C:\Scripts\Test.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again

Every .ps1 script run by powershell spawned by running a .vbs this way results in this error.

The .ps1 file works fine when opened manually in powershell

Upvotes: 0

Views: 625

Answers (1)

LesFerch
LesFerch

Reputation: 1981

The actual error is more revealing: enter image description here

This is not a Windows 11 issue. I tested your script and found that it failed to execute the ps1 file on my Windows 10 machine. After retyping the script, it worked fine. The difference: ANSI vs UTF-8 encoding.

When you change the encoding to ANSI (e.g. using NotePad), the path will become ??C:\Scripts\Test.ps1. Just delete the question marks and save. It should now work correctly.

Note: The string C:\Scripts\Test.ps1 is exactly the same encoding for ANSI and UTF-8. The problem (based on the text posted in the question) is that this string was preceded by E2 80 AA (U+202A, Left-To-Right Embedding). Removing that invisible character fixes the path.

Note: As stated in the comments, you should use -file instead of -command but it will work either way once the encoding is corrected.

Upvotes: 0

Related Questions