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" )
Matches()method operates on a string (like in your PowerShell example) whereas in your batch example you try to have it operate on a file. The latter doesn't work, so your (batch) variable is not set. Is the string in the PowerShell example representative for the content of your files (i.e. are all key=value pairs on the same line)? Or are they one key=value pair per line? Please update your question with a representative input sample.tmpFileor is the file generated by someone/something else? Can you pre-process the file to get a format with one key=value pair per line?