Search code examples
windowspowershellcmdcommandautoit

Cannot parse path with spaces to powershell


I am trying to fire a filesearch from Autoit via powershell to an already created line.

This is the full run command ( https://www.autoitscript.com/autoit3/docs/functions/Run.htm ) I am trying to go with :

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-ChildItem -File D:\__Temp\123\2 - Copy -Recurse -Filter *.dcm | ForEach-Object {  '<image>' + $_.FullName + '</image>' } | Out-File -FilePath D:\Temp\scan.txt -Append -Encoding UTF8

I already tried to use several " or ' around the path but nothing seemed to work and the shell just exits instantly without the filewrite.

Here's what I've tried:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-ChildItem -File 'D:\__Temp\123\2 - Copy' -Recurse -Filter *.dcm | ForEach-Object {  '<image>' + $_.FullName + '</image>' } | Out-File -FilePath D:\Temp\scan.txt -Append -Encoding UTF8

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command Get-ChildItem -File "D:\__Temp\123\2 - Copy" -Recurse -Filter *.dcm | ForEach-Object {  '<image>' + $_.FullName + '</image>' } | Out-File -FilePath D:\Temp\scan.txt -Append -Encoding UTF8

Solution

  • Thanks to the tip from mklement0 this is the working solution:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "Get-ChildItem -File 'D:\__Temp\123\2 - Kopie  dads ds a' -Recurse -Filter *.dcm | ForEach-Object {  '<image>' + $_.FullName + '</image>' } | Out-File -FilePath C:\Users\LUKAS~1.WIL\AppData\Local\Temp\DB_Tool_jDicomCC\scan.txt -Append -Encoding UTF8"
    

    That is, the entire -Command argument is enclosed in "...", and the embedded path with spaces is enclosed in '...'.