Link to home
Create AccountLog in
Windows OS

Windows OS

--

Questions

--

Followers

Top Experts

Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)🇺🇸

Passing Quoted Parameter to NirCmd elevated Command
Trying to pass a quoted parameter (file path) to NirCmd elevate from a bat script. This works:

"C:\...\nircmdc.exe" elevate "C:\...\Script.bat" C:\Test.txt

This does not:

"C:\...\nircmdc.exe" elevate "C:\...\Script.bat" "C:\Test.txt"

How can I pass a quoted parameter to NirCmd elevate?

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of HainKurtHainKurt🇨🇦

maybe this

"C:\...\nircmdc.exe" elevate "C:\...\Script.bat" 'C:\Test.txt' 

Avatar of HainKurtHainKurt🇨🇦

I created a bat file and put this into bat
C:\Utility\NIRSoft\NirSoft\nircmd.exe elevate "notepad.exe" 'C:\temp\1.txt'

Open in new window

then run that bat file from command line
it works fine...
what does your bat file look like...

Avatar of HainKurtHainKurt🇨🇦

and this also works

C:\Utility\NIRSoft\NirSoft\nircmd.exe elevate "notepad.exe" 'C:\temp\1.txt'

C:\Utility\NIRSoft\NirSoft\nircmd.exe elevate "notepad.exe" """C:\temp\2.txt"""

Open in new window


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of zorvek (Kevin Jones)zorvek (Kevin Jones)🇺🇸

ASKER

That works but the parameter gets buggered before it is passed to the .bat script if the path has any spaces:

"C:\...\nircmdc.exe" elevate "C:\...\Script.bat" 'C:\Test File.txt' 

Avatar of HainKurtHainKurt🇨🇦

this worked for me

C:\Utility\NIRSoft\NirSoft\nircmd.exe elevate "notepad.exe" """C:\temp\1 - Test.txt"""

Open in new window


Avatar of HainKurtHainKurt🇨🇦

this one also worked

C:\Utility\NIRSoft\NirSoft\nircmd.exe elevate "notepad.exe" 'C:\temp\1 - Test.txt'

Open in new window


Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of HainKurtHainKurt🇨🇦

maybe the issue is bat file
what do you have in bat file?

Script.bat

In your Batch are you using %1 or %~1 or %~f1  ?

Zorbek Does you nircmd path contain spaces?
Can you put your nircmd somewhere without spaces or add its location to your PATH variable?



Avatar of HainKurtHainKurt🇨🇦

I guess using " in your bat file resolves the issue...

like

notepad.exe %1
>>>
notepad.exe "%1"



Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


When I use single quotes it calls the second script but the file name is split at the first space. When I use double quotes the second script isn't called.

My first script "Test Script 1.bat":

SET FilePath=C:\Users\Kevin Jones\Desktop\Test\Test File.txt
"C:\Users\Kevin Jones\Desktop\Test\nircmdc.exe" elevate "C:\Users\Kevin Jones\Desktop\Test\Test Script 2.bat" '%FilePath%'
PAUSE

Open in new window


My second script "Test Script 2.bat"

ECHO %1%
PAUSE

Open in new window


I'm not sure why but your notepad example with double quotes works. But when I use double quotes on the file path parameter in my script, the second script is never called.

Avatar of HainKurtHainKurt🇨🇦

ok, I created same files and edited...
now what are you doing, how do you call and which one
and what should happen and what is not working...

Drop the second %
just use %1

also double quotes are the correct way, try instead using them around your
SET "FilePath=C:\Users\Kevin Jones\Desktop\Test\Test File.txt"

there is something else I believe is happening, but need to test first

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


I am running Test Script 1.bat first. It runs nircmdc.exe with which I am trying to run Test Script 2.bat.

If I drop the second % then the variable substitution doesn't happen.

Any time I add double quotes the second script is not invoked.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


The objective is to have the first script call the second script using nircmd's elevate function and pass a file path that can include spaces. The parameter needs to be passed to the second script as the first parameter in it's entirety without being broken apart by those embedded spaces.

When a double quote is passed to nircmd after the first parameter (the script name) it fails to invoke the second script.

When single quotes are used, the second script is called but the file path is broken up by any embedded spaces and only part of the path is received as the first parameter.

Avatar of HainKurtHainKurt🇨🇦

looks like nircmd elevate is not working to call bat files...

Test Script 1.bat
SET FilePath=C:\temp\Test File.txt
SET cmd="C:\temp\Test Script 2.bat" "%FilePath%"
echo %cmd%
"C:\Utility\NIRSoft\NirSoft\nircmd.exe" elevate %cmd%
PAUSE script 1...

Open in new window

Test Script 2.bat 
ECHO %1%
notepad %1%
PAUSE script 2...

Open in new window

command line
"Test Script 1.bat"

Open in new window

no error, but no notepad, it closes immediately...
User generated image

Avatar of HainKurtHainKurt🇨🇦

when I use this, it works, notepad opens the file...
SET FilePath=C:\temp\Test File.txt
SET cmd="C:\temp\Test Script 2.bat" "%FilePath%"
SET cmd="notepad.exe" "%FilePath%"
echo %cmd%
"C:\Utility\NIRSoft\NirSoft\nircmd.exe" elevate %cmd%
PAUSE script 1...

Open in new window


User generated image

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of HainKurtHainKurt🇨🇦

even this, some spaces in the program part, works fine

SET cmd="C:\Program Files\Windows NT\Accessories\wordpad.exe" "%FilePath%"

Open in new window

but bat file fails...

Our tests work fine as long as we are not calling a .bat file. Here are the results of my tests:

REM This calls the .bat script but the system breaks apart the path into multiple parameters
SET FilePath=C:\Windows\System32\Test File.txt
SET Command="C:\Windows\System32\Test Script.bat" '%FilePath%'
"C:\Windows\System32\nircmdc.exe" elevate %Command%

REM This does not call the .bat script at all
SET FilePath=C:\Windows\System32\Test File.txt
SET Command="C:\Windows\System32\Test Script.bat" "%FilePath%"
"C:\Windows\System32\nircmdc.exe" elevate %Command%

REM This runs Notepad and the file is opened
SET FilePath=C:\Windows\System32\Test File.txt
SET Command="C:\Windows\System32\Notepad.exe" '%FilePath%'
"C:\Windows\System32\nircmdc.exe" elevate %Command%

REM This runs Notepad and the file is opened
SET FilePath=C:\Windows\System32\Test File.txt
SET Command="C:\Windows\System32\Notepad.exe" "%FilePath%"
"C:\Windows\System32\nircmdc.exe" elevate %Command%

Open in new window


My conclusion is that, for whatever reason, nircmd's elevate function has problems with .bat files that it does not have with .exe files. If the file path parameter is enclosed in double quotes, or ay double quote used after the .bat file reference, the .bat is not called at all. If the file path parameter is enclosed in single quotes or no quotes are used, the .bat file is called but the file path parameter is parsed into multiple parameters split by spaces.

One workaround is to use single quotes or no quotes and use the %* parameter reference in the invoked .bat file but that only allows for one parameter.

Avatar of HainKurtHainKurt🇨🇦

I concluded the same thing before...
elevate bat file with nircmd does not work...
any other executabble works...

either we need to check `run-as` or find another utility (I guess all others will do the same thing -v will not work with bat)
or you need to use some other logic...
maybe create a tiny exe instead of bat file


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Avatar of zorvek (Kevin Jones)zorvek (Kevin Jones)🇺🇸

ASKER

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account
Windows OS

Windows OS

--

Questions

--

Followers

Top Experts

This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.