2

I have following lines in my Batch script which in turn calls Powershell command to self-elevate the batch file as Admin.

@echo off && setlocal
if not "%1"=="admin" (powershell start -verb runas '%0' admin & pause & exit /b)
....
....

But the Batch file path itself and as result the '%0' contains spaces in the %USERNAME%(Vivek Shah) especially, and that's why when invoking this script, it throws error:

The term 'C:\Users\Vivek' 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.
At line:1 char:1

How do I handle spaces in '%0' and make this script working perfectly ??

Already tried '%~0' & '"%0"' but both apparently don't work.

Vicky Dev
  • 1,893
  • 2
  • 27
  • 62
  • Have you tried `' "%0" '` (ie. enclose `%0` in `"` quotes? – Magoo Aug 09 '23 at 00:37
  • Yes @Magoo tried that too, still same error. – Vicky Dev Aug 09 '23 at 00:39
  • 1
    OK - tried `%~s0` to use the short name (assuming you have short-name generation enabled) ? – Magoo Aug 09 '23 at 01:03
  • That did work like charm, thank you @Magoo. You wanna re-brand this as a working answer ? You can also mention that in answer, what are other options if short-name generation is not enabled, or how to enable it, if there aren't any other options – Vicky Dev Aug 09 '23 at 01:05

1 Answers1

1
  • Use %~f0 to refer to the running batch file's full path.

    • Run cmd /c call /? for an explanation of the syntax.
  • To ensure that this path is passed as-is to PowerShell's Start-Process (whose built-in aliases are start and saps), enclose it in \"..."\ (the \-escaping is necessary in order for the (implied) -Command argument passed to powershell.exe, the Windows PowerShell CLI, to pass the " characters through as part of the command to execute).[1]

@echo off && setlocal

if not "%1"=="admin" (powershell start -verb runas \"%~f0\" admin & pause & exit /b)

[1] Note: Without enclosing the implied -Command argument in "..." overall means that the value of ~%f0 is subject to whitespace normalization, meaning that runs of multiple whitespace characters are folded into a single space each. However, that is rarely a problem in practice. Just adding an overall "..." enclosure (powershell "start ..." & pause & exit /b) is not necessarily enough, unfortunately, because it requires individually ^-escaping any cmd.exe metacharacters inside \"...\". In the rare event that whitespace normalization is a problem, with powershell.exe you can use "^""..."^"" (sic) inside overall "...", but only outside for /f loops - see this answer. Fortunately, in PowerShell (Core) 7+, whose CLI is pwsh.exe, the robust solution is simpler: both outside and inside for /f, use overall "..." enclosure with embedded ""..."" quoting.

mklement0
  • 382,024
  • 64
  • 607
  • 775