I am running a powershell command -encoded via a CMD file, because of the way I am running it I have to build out the $Args var from a passed %* from CMD. I do a text replace with CMD before launching at the top of the PS1 code that would end up being something like this:
$p='%*'; $Args=$p.Split(" ")
This gives me an array with space as a delimiter, but this obviously misses things like "paths with spaces" etc because every word gets split. CMD automatically wraps double quotes (") around each arg with spaces.
I know I can do regex with -Split() instead of .Split(), I could use no regex for one space as shown, or regex for anything in between quotes, but I'm not sure how to do both in one shot.
This is an example using the above method:
<# ::
@ECHO OFF
POWERSHELL -nop -ep bypass -c "POWERSHELL -nop -ep bypass -en ([System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((GC '%~f0' -raw) -Replace('###POWERSHELL BELOW THIS LINE###','$PSScriptRoot=''%~dp0'';$PSCommandPath=''%~f0'';Set-Location ''%~dp0''; $p=''%*''; $Args=$p.Split(""" """)'))))"
GOTO :EOF
#>
###POWERSHELL BELOW THIS LINE###
"There are a total of $($args.count) arguments"
$Args | % { 'arg #{0}: [{1}]' -f ++$i, $_ }
cmd /c pause
UPDATED: Found a working solution
<# ::
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION & FOR %%a IN (%*) DO SET ARGS=!ARGS!%%a,
POWERSHELL -nop -ep bypass -c "POWERSHELL -nop -ep bypass -en ([System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((GC '%~f0' -raw) -Replace('###POWERSHELL BELOW THIS LINE###','$PSScriptRoot=''%~dp0'';$PSCommandPath=''%~f0'';Set-Location ''%~dp0''; $p=''!ARGS:~0,-1!''; $Args=$p.Split(""",""")'))))"&ENDLOCAL
GOTO :EOF
#>
###POWERSHELL BELOW THIS LINE###
"There are a total of $($args.count) arguments"
$Args | % { 'arg #{0}: [{1}]' -f ++$i, $_ }
cmd /c pause
pausecommand too (no need forcmd /c pause): it is a function wrapper aroundRead-Host, specifically:$null = Read-Host 'Press Enter to continue...'