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
6
  • 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" Commented Apr 7, 2022 at 12:53
  • Still the samething Commented Apr 7, 2022 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. Alternatively, for individual scripts, you can create shortcut files that launch them, as shown in ryanconmeo's answer Commented Apr 7, 2022 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""" Commented Apr 7, 2022 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. Commented Apr 7, 2022 at 17:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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"""

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.