- Just replace....
1) %comspec% + -ArgumentList '/c + %~fnx0 % to:
Start-Process $env:windir\system32\cmd.exe -Verb RunAs -Argument $env:cmd_arg
2) Your powershell command does not require quotes, remove them
%__APPDIR__%\WindowsPowerShell\v1.0\powershell.exe -ex unrestricted -Command Start-Process $env:windir\system32\cmd.exe -Verb RunAs -Argument $env:cmd_arg
3) Remove quotes/semi-quotes from '/c %~fnx0 %*' and replace to $env:cmd_arg
4) For %%P1%%, try using:
cmd /v /c "echo !P1!"
call echo/ %P1%
echo/ %P1%
echo= %P1%
echo[ %P1%
echo( %P1%
echo. %P1%
@echo off && setlocal EnableDelayedExpansion
title <nul && Title ...\%~nx0
%__APPDIR__%mode.com con: cols=99 lines=3
for %%i in (%*)do set "_arg=!_arg! "%%~i" "
set "cmd_arg= /v /c "%~fnx0" !_arg!" && %__APPDIR__%Net.exe file 2>nul >nul && (
goto :RunAsAdmin
) || (
color 9f & echo/ Running your PowerShel cmdlets...
set "_ps1=%__APPDIR__%\WindowsPowerShell\v1.0\powershell.exe"
!_ps1! -ex unrestricted -Command Start-Process $env:windir\system32\cmd.exe -Verb RunAs -Argument $env:cmd_arg
echo/ Press any key to exit... && %__APPDIR__%timeout.exe -1 >nul & endlocal && goto :EOF
)
echo/ If you see this, sorry it doesn't work... && goto :EOF
:RunAsAdmin
%__APPDIR__%mode.com 99,25 && color 0A
echo/If you see this one, it works sr. Admin^!!...
title <nul && Title ...\%~nx0
rem :: do your admin tasks here....
echo/ Press any key to exit... && %__APPDIR__%timeout.exe -1 >nul
endlocal && goto :EOF
Answer from Io-oI on Stack Exchangepowershell - %temp% etc not working - Stack Overflow
Environment Variable inside a single '
Adding non-expanded environment variable to Path, but it doesn't expand
Set and Get windows environment variables from powershell
When you start a string literal with ' (single-quotes), you're creating a verbatim string - that is, every character inside the string is interpreted literally and variable references and expressions won't expand!
Use " if you want the variable to be expanded:
$var = 'C:'
Get-WmiObject -Class win32_logicaldisk -Filter "DeviceID='$var'"
If your variable name has weird characters, or is followed by a word character, you can qualify the variable name with curly brackets {} immediately after the $:
$var = 'C:'
Get-WmiObject -Class win32_logicaldisk -Filter "DeviceID='${var}'"
If, for instance, you're getting data from a file with select-string, the return is a single quote string. If that string contains variables they won't expand. Invoke-Expression can be used if the variable is clean - not mixed in with other text:
a = '
a -> 123
If the variable is part of a path name, this doesn't work. Use $ExecutionContext.InvokeCommand.ExpandString($var)
$path = '$Home/HiMum'
$ExecutionContext.InvokeCommand.ExpandString($path)
-> /home/JoeBlogs/HiMum
You lucky sods on Windows might be able to use Convert-String to change singles to doubles.
If you are executing that line from PowerShell rather than from CMD, you can use the PowerShell environment variable syntax:
PS C:\> & "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
You can use [Environment]::ExpandEnvironmentVariables to expand environment variables within a string the old-fashioned way.
$s = '%systemroot%\temp'
[Environment]::ExpandEnvironmentVariables($s)
Hi Everyone,
I am not great with scripting, so apologies for not using the correct terminology and likely asking a very basic question. I am attempting to install an MSI package and my MSI package has a space in the name (I can't rename it, it has to be this name). I am invoking the start-process command with the below argument list.
-ArgumentList '/I "$env:temp\My MSI File.msi" /qn'
This fails and I believe it is due to the use of the single quote which I think it turning everything inside of it as a literal statement so it is not reading the environment variable etc. What do I need to do to get this to work?
Cheers!
***Edit, I figured it out. I needed to wrap what needed to be in quotes with an extra set of quotes and use quotes around the entire argument list like this:
-ArgumentList "/I ""$env:temp\My MSI File.msi"" /qn"
I was trying to add a environment variable to my user's Path using PowerShell, so I searched about and ended up with a code similar to this one:
[Environment]::SetEnvironmentVariable("TEST_VAR", "test-value", "User")
$currPath = [Environment]::GetEnvironmentVariable("Path", "User")
$newPath = "$currPath;%TEST_VAR%;"
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
The idea is to change TEST_VAR value in the future and by doing so updating the Path easily.
The problem I've found is that when setting Path with a non-expanded variable (in this case, %TEST_VAR%) it won't expand automatically. So, if I open the cmd and try to echo Path, I get something like this:
C:\Progr [...] omeOherStuff;%TEST_VAR%;
When it should be returning this:
C:\Progr [...] omeOherStuff;test-value;
Some places where I searched said that the answer to this problem is messing around with reg add, but I am not sure about it and even with some testing trying to set %TEST_VAR% as REG_EXPAND_SZ didn't work for me.
That is my first post here, also sorry if my english is bad. Thanks.