1

i have a script to run and he works fine when i use that on PowerShell ISE, but i need to run at .ps1 file (double clicking) and when i try this, hothing happen... the powershell's window opens but my script dont run.

my code is:

$path = "W:\Processos\Integracao\Titulos Carrefour"
$path2 = "\\fileserver\departamentos$\Matriz\Titulos Carrefour" 
    
    
Set-Location $path
        
        
Get-ChildItem -Path "$path2\*.csv" -Recurse | Move-Item -Destination $path -Force 
        
        
Start-Process 'winscp.com' -ArgumentList /script=envio.scp -Wait
  • 3
    Ps1 is not an executable. If you want to run it by double clicking you can set up a shortcut using powershell.exe -F "pathtoscript" – Santiago Squarzon Apr 07 '22 at 12:53
  • Still the samething – Lucas Crespo Ferreira Apr 07 '22 at 13:27
  • 1
    By default, `*.ps1` file are _opened for editing_ when you double-click them in File Explorer. Changing that _interactively_ to using `powershell.exe` to open such files (a) doesn't keep the window that the script runs in open afterwards and (b) fails with script paths that contain spaces and single quotes. To robustly make `*.ps1` files execute in a stay-open window, a _programmatic_ solution is necessary, as shown in [this answer](https://stackoverflow.com/a/58245206/45375). Alternatively, for individual scripts, you can create shortcut files that launch them, as shown in ryanconmeo's answer – mklement0 Apr 07 '22 at 13:50
  • I found something like this, but calling the scrip with a .BAT... i will let the code below: I found a solution... for who ever have the same problem with powershell 4.0, call the script whit a .BAT, it works! i will let the code below: powershell -noexit "& ""C:\my_path\yada_yada\run_import_script.ps1""" – Lucas Crespo Ferreira Apr 07 '22 at 15:07
  • @LucasCrespoFerreira, that is just as cumbersome as creating a shortcut file for each `.ps1` script, but, yes, `.cmd` / `.bat` files _are_ executed when double-clicked from File Explorer, unlike `.ps1` files. – mklement0 Apr 07 '22 at 17:11
  • That said, situationally a companion batch file may be the right solution, so I encourage you to post your solution _as an answer_. Tip: Use something like `powershell.exe -NoExit -File "C:\my_path\yada_yada\run_import_script.ps1" %*` instead. – mklement0 Apr 07 '22 at 17:22

1 Answers1

2

You can run .ps1 files by right clicking and selecting "Run with PowerShell". If you want to run PowerShell scripts by double clicking, you can create a shortcut:

On Windows,

  1. Right click in your file explorer and create a shortcut
  2. When it asks for location, type: powershell.exe -F "pathofscript". So if your script is located in C:\asdf\hello.ps1, you would type:
    powershell.exe -F "C:\asdf\hello.ps1"

Now you have a shortcut that will run your script when double clicked.

Note: If you move your .ps1 script somewhere else, you'll need to update the shortcut to reflect that.

mklement0
  • 382,024
  • 64
  • 607
  • 775
ryanconmeo
  • 36
  • 3
  • 1
    Helpful, but it's worth pointing out that this is isn't a _general_ solution for directly executing _all_ `.ps1` files by double-clicking. It requires you to create a separate companion shortcut file for each and every `.ps1` script you want to execute this way. – mklement0 Apr 07 '22 at 14:12
  • I found a solution... for who ever have the same problem with powershell 4.0, call the script whit a .BAT, it works! i will let the code below: powershell -noexit "& ""C:\my_path\yada_yada\run_import_script.ps1""" – Lucas Crespo Ferreira Apr 07 '22 at 15:05