Getting a button with powershell to work

I'm hope someone can help with what I think is a simple thing that is just elluding me, I would lke to have a button that runs a line of powershell as one part of it and I want to aviod exteranl powershell scripts if possible.

The below line is what I am trying (with variations on the {sourcepath} or it not being there at all). I know the command works from a cmd or directly from powershell so I am sure the problem is the path referenceing but I can't figure out what I need to change

powershell "gci {sourcepath}\*_guetzli.jpg | % { Move-Item $_ $_.Name.Replace('_guetzli','') -Force }"


{sourcepath} has a backslash in the end of it (unless |noterm is added) so you're sending two backslashes after it, which may be the problem.

Many thanks, didn't solve the issue (may have partly done) but made me look at elsewhere when it still didn't work. I ended up using the below, the problem was I had a % in the command, I just replaced it with the long hand "ForEach-Object"

powershell "gci -Path {sourcepath$} -Filter *guetzli* | ForEach-Object { Move-Item $_ $_.Name.Replace('_guetzli','') -Force }"

correction I had to use

powershell "gci -Path '{sourcepath$}' -Filter *guetzli* | ForEach-Object { Move-Item $_ $_.Name.Replace('_guetzli','') -Force }"

for it to work in more/all cases. the {sourcepath$} is now surrounded by ' ' to handle spaces and special charactors like ( )

What happens if there's a ' in the source path?

In PS scripts, '{sourcepath$}' can be replaced with ..

In batch files, % needs to be doubled: %%.

Would that need a cd {sourcepath} line before the powershell line to ensure the current directory is correct? I think it will be System32 otherwise.

No, if it's a DOS button, Opus will hand over the source to powershell.exe. And yes, in a standard button, it will be system32.

1 Like