I am creating a dos batch script (cmd.exe) .cmd
which through powershell elevates itself as administrator.
But if it's stored in a folder that contains both spaces and ampersand in its name, it won't work.
I am not an expert so I searched the web, but I could not find a solution or documentation to solve the problem. So I would also like to know how to debug this particular situation and the possible solutions to the problem and where to find the documentation describing the solution or the steps to get there .. (if any).
To recreate the problem: I created a folder under the desktop named "Pie & tea" without quotes. And in the folder I created a script "test.cmd" inside which I put only the essential to reproduce the problem. This is the content of the script:
@echo off
pause
PowerShell start "%~f0" -verb runas
pause
goto: eof
I run the script by double clicking on it (this is the desired mode). An error is shown with this code (I translate from italian with a translator):
Press any key to continue. . .
In row: 1 car: 34
+ start C:\Users\fposc\Desktop\Pie & tea\test.cmd -verb runas
+ ~
Ampersand (&) not allowed. The & operator is reserved for future use. Use "&" to pass the ampersand as
string.
+ CategoryInfo: ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId: AmpersandNotAllowed
Press any key to continue. . .
And following some instructions I modified the line of code with powershell by tripling the double quotes:
PowerShell start """%~f0""" -verb runas
But the cmd window opens and after pressing the space the UAC starts and after giving the consent another window appears for a moment but it closes immediately and I could not see any errors.
I have seen on the web any solutions using single quotes '
or the powershell escape character `
or the caret ^
of the dos batch or the --%
parameter of the powershell etc.
But i was unable to resolve.
I do not put the attempts made here because I copied in a stupid way (since I am not familiar with the mixed cmd / powershell operation) and I made numerous random attempts combining the various solutions. Without any positive outcome.
I have also seen solutions that I would like to evaluate, such as:
- change directories and use relative paths.
- prepare string variables and replace characters.
- Use powershell -encodedcommand to pass parameters.
But the preferred solution is the one that explains how to escape characters on the single line of code
PowerShell start "%~f0" -verb runas
and how to get there and the reference documentation.
Some links visited:
VBS Shell.Application and PowerShell Start-Process do not escape ampersands internally
Quoting issues with PowerShell
Issues on windows when the username contains &
Executing PowerShell script via explorer context menu on items containing ampersands in their names
Stackoverflow: batch file deal with Ampersand (&) in folder name
Stackoverflow: How to access file paths in PowerShell containing special characters
Stackoverflow: How to pass strings to powershell from cmd with ampersands in values?