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.
This works
@echo off powershell "cat -raw C:\examplenospace\test.txt | invoke-expression"
This works
@echo off set "file=C:\examplenospace\test.txt" powershell "cat -raw %file% | invoke-expression"
Not working
@echo off set "file=C:\example with space\test.txt" powershell "cat -raw %file% | invoke-expression"
This also doesn't work of course
@echo off powershell "cat -raw "C:\example with space\test.txt" | invoke-expression"
Also doesn't work
@echo off powershell ""cat -raw "C:\example with space\test.txt" | invoke-expression"
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?