Reputation: 23
I would like to run a powershell command with batch. My powershell command:
gc 'C:\Users\Test\Test-01\source.csv' | %{
$cols = $_.split(";")
1..$cols[-2]| %{
"$($cols[0..9] -join ';');$($cols[(9+$_)] -join ';');$($cols[(24+$_)] -join ';');;;;;;;;;;;;;;;$($cols[-2]);"
}
} | sc "C:\Users\Test\Test-01\target.csv"
My batch command:
powershell -Executionpolicy ByPass gc 'C:\Users\Test\Test-01\source.csv' | %{
$cols = $_.split(";")
1..$cols[-2]| %{
"$($cols[0..9] -join ';');$($cols[(9+$_)] -join ';');$($cols[(24+$_)] -join ';');;;;;;;;;;;;;;;$($cols[-2]);"
}
} | sc "C:\Users\Test\Test-01\target.csv"
It doesn't work. But why? Can someone say why?
Upvotes: 0
Views: 214
Reputation: 67216
To embedd PowerShell code into a .BATch file you just need to pay attention to a few details:
-ExecutionPolicy
and -Command
stuff. Just write pure powershell code after powershell
command.^
;
\"like this\"
or change they by apostrophe: 'like this'
And these modifications are required because Batch file special characters:
%%
| > < &
with caret, like this: ^| ^> ^< ^&
In your case this is the final, working code:
powershell ^
gc 'C:\Users\Test\Test-01\source.csv' ^| %%{ ^
$cols = $_.split(\";\"); ^
1..$cols[-2]^| %%{ ^
\"$($cols[0..9] -join ';');$($cols[(9+$_)] -join ';');$($cols[(24+$_)] -join ';');;;;;;;;;;;;;;;$($cols[-2]);\" ^
} ^
} ^| sc \"C:\Users\Test\Test-01\target.csv\"
You may also review this question.
Upvotes: 1