Path issue with passing a command from Autohotkey to PowerShell

Mypath := "C:\temp\example1.txt"
Run, powershell -command "get-content -path %Mypath% | Set-Clipboard"

The above works just fine, content is copied to the clipboard by PowerShell but when I do the same thing but with a file that has a space in its file name it fails to run in PowerShell.

Running below

Mypath := "C:\temp\example 1.txt"
Run, powershell -command "get-content -path %Mypath% | Set-Clipboard"

I get this error in PowerShell

Get-Content : A positional parameter cannot be found that accepts argument '1.txt'.

I have also tried to use double quotes on each side and use Autohotkeys escape symbol like so:

Mypath = "`"C:\temp\example 1.txt`""
Run, powershell -command "get-content -path %Mypath% | Set-Clipboard"

As well as

Mypath = ""C:\temp\example 1.txt""
Run, powershell -command "get-content -path %Mypath% | Set-Clipboard"

But on both occasions, PowerShell throws this error;

The string is missing the terminator: ".

What am I doing wrong? any help would be appreciated.

Hellen,
Welcome to the forum. :wave:t4:

Is the code you posted just an example or do you really just try to read the content of a file and put it to the clipboard? You know you could do this with pure Autohotkey code, don’t you? :wink:

If it’s just an example you could try the following way. According the to help you get with

PowerShell /?

you can run commands enclosed in curly braces. And since you don’t need double quotes to enclose the complete command you can use them to enclose the path inside this command.

Mypath := "C:\temp\example 1.txt"
Run, powershell -command {get-content -path "%Mypath%" | Set-Clipboard}