0

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

Kitten
  • 1
  • 1
  • That is very curious. With the details currently contained in your question, there's no good explanation for your symptom. Consider updating your question with additional information that may be relevant (in the abstract: local vs. remote invocation, ...). – mklement0 Jan 24 '22 at 23:50
  • You are using `-command` which expects a script block not the path to a file use `-File` instead. See [about_PowerShell_exe](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1). – user692942 Jan 25 '22 at 00:08
  • Does this answer your question? [Running Powershell from vbs with command as parameter](https://stackoverflow.com/questions/11448365/running-powershell-from-vbs-with-command-as-parameter) – user692942 Jan 25 '22 at 00:17
  • @user692942 No. I should of used ```-file``` but it will work fine without it. Doesn't fix anything. – Kitten Jan 25 '22 at 01:17
  • It looks like there's a strange unicode character right before the filename. – js2010 Feb 04 '22 at 19:29

1 Answers1

0

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.

LesFerch
  • 1,540
  • 2
  • 5
  • 21