.ps1 script dont run at double click [duplicate]
Asked Answered
B

1

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
Blimp answered 7/4, 2022 at 12:50 Comment(6)
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"Dodona
Still the samethingBlimp
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. Alternatively, for individual scripts, you can create shortcut files that launch them, as shown in ryanconmeo's answerBlades
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"""Blimp
@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.Blades
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.Blades
B
3

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.

Bacillus answered 7/4, 2022 at 13:27 Comment(2)
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.Blades
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"""Blimp

© 2022 - 2024 — McMap. All rights reserved.