简体   繁体   中英

File path with spaces not working in batch file

I am trying to cat a text file containing a PowerShell script from a.bat file into PowerShell running within the command line.

It's kind of a workaround to start a PowerShell script on startup without having permission on the system.

It works perfectly fine until I try paths with spaces. Does someone know how to make this work?

I searched and tried many solutions (see 3,4,5,6) but either this is a very specific case, or I am doing something wrong. My knowledge of scripts in general is lacking some, but I'm trying my best. Please use simple language in your answer.

  1. This works

    @echo off powershell "cat -raw C:\examplenospace\test.txt | invoke-expression"
  2. This works

    @echo off set "file=C:\examplenospace\test.txt" powershell "cat -raw %file% | invoke-expression"
  3. Not working

    @echo off set "file=C:\example with space\test.txt" powershell "cat -raw %file% | invoke-expression"
  4. This also doesn't work of course

    @echo off powershell "cat -raw "C:\example with space\test.txt" | invoke-expression"
  5. Also doesn't work

    @echo off powershell ""cat -raw "C:\example with space\test.txt" | invoke-expression"
  6. I tried many variations, nothing seems to work

    @echo off powershell "cat -raw \"\"C:\example with space\test.txt" | invoke-expression"

How can I make it work?

@Compo thank your for your answer, this worked straight away, both of them!

@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "Get-Content -LiteralPath 'C:\example with space\test.txt' -Raw | Invoke-Expression"

@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "Get-Content -LiteralPath "C:\example with space\test.txt" -Raw | Invoke-Expression"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM