It's all about proper quoting and escaping. Read powershell -?
(excerpt truncated):
-Command
Executes the specified commands (and any parameters) as though they were
typed at the Windows PowerShell command prompt, and then exits, unless
NoExit is specified. The value of Command can be "-", a string. or a
script block.
…
If the value of Command is a string, Command must be the last parameter
in the command , because any characters typed after the command are
interpreted as the command arguments.
To write a string that runs a Windows PowerShell command, use the format:
"& {<command>}"
where the quotation marks indicate a string and the invoke operator (&)
causes the command to be executed.
Here our <command>
contains double quotes:
$datePattern = [Regex]::new('(\d\d\.\d)');$matches = $datePattern.Matches("/ start=2010 / height=1 / value=12.2 / length=0.60 / users=264 / best=Adam /");$matches.Value
Use single quotes instead as follows:
$datePattern = [Regex]::new('(\d\d\.\d)');$matches = $datePattern.Matches('/ start=2010 / height=1 / value=12.2 / length=0.60 / users=264 / best=Adam /');$matches.Value
or, alternatively, double inner double quotes twice:
$datePattern.Matches(""""/ … / value=12.2 / … /"""")
Full powershell call then looks as follows:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {$datePattern = [Regex]::new('(\d\d\.\d)');$matches = $datePattern.Matches(""""/ start=2010 / height=1 / value=12.2 / length=0.60 / users=264 / best=Adam /"""");$matches.Value}"
Finally, apply FOR /F
Loop command: against the results of another command:
for /f "usebackq" %i in (`PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {$datePattern = [Regex]::new('(\d\d\.\d)');$matches = $datePattern.Matches(""""/ start=2010 / height=1 / value=12.2 / length=0.60 / users=264 / best=Adam /"""");$matches.Value}"`) do ( set "newValue=%i" )
The latter command works from a command prompt. Double the %
sign in a batch script:
for /f "usebackq" %%i in (`PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {$datePattern = [Regex]::new('(\d\d\.\d)');$matches = $datePattern.Matches(""""/ start=2010 / height=1 / value=12.2 / length=0.60 / users=264 / best=Adam /"""");$matches.Value}"`) do ( set "newValue=%%i" )