1

I have this simple PowerShell script:

Copy-Item -path ('\\PC1\Images\' + (Get-ChildItem -Path '\\PC1\Images' -Attributes !Directory *.dat | Sort-Object -Descending -Property LastWriteTime | Select-Object -first 1)) 'C:\Temp\PC1\screen.jpg' | Start-Process 'C:\Program Files\Google\Chrome\Application\chrome.exe' -ArgumentList 'file:///C:/Temp/PC1/screen.jpg'

If I run this command from a PowerShell window, it works fine. If I save it as .ps1 file and run it from Windows Explorer, nothing happens (Chrome does not open the desired URL).

What can be done to allow to execute this script from some shortcut? (I want to add a shortcut/icon to my Windows taskbar for fast access).

Ωmega
  • 42,614
  • 34
  • 134
  • 203

2 Answers2

2

You have two options:

  • Either: Make .ps1 files themselves - which by default are opened for editing when opened (double-clicked) from File Explorer / the desktop / the taskbar - directly executable:

    • See this answer.

    • Important: Doing this will make all .ps1 files you drag to the taskbar be associated with a single icon, for PowerShell itself, with the dragged files getting pinned to that icon; that is, to run such a file you'll have to do so via the PowerShell icon's shortcut menu.

  • Or: For a given .ps1 file, create a companion shortcut file (.lnk) or batch file (.cmd) that launches it, via PowerShell's CLI (powershell.exe for Windows PowerShell, pwsh.exe for PowerShell (Core) 7+)

    • Note: Once you've created a companion file as described below, dragging it to the taskbar will give it is own taskbar icon.

    • Batch-file approach:

      • Create a companion batch file in the same folder as your .ps1, with the same base file name; e.g., foo.cmd alongside foo.ps1

      • Add the following content to the .cmd file (add -NoProfile and -ExecutionPolicy Bypass as needed):

         @powershell.exe -NoExit -File "%~dpn0.ps1" %*
        
    • Shortcut-file approach:

      • See this answer for how to create such a shortcut file interactively.

      • See this answer for how to create shortcut files programmatically.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Association of *.ps1* is not an issue. I have set *.ps1* files being opened by `powershell.exe`, so I can execute these files. However, they don't run the same as if I enter the code in a PowerShell window. The `Start-Process 'C:\Program Files\Google\Chrome\Application\chrome.exe' ...` is not executed, or it is halted, as I don't see Chrome to open the URL if I run it outside of PowerShell window. – Ωmega Apr 27 '22 at 22:11
  • @Ωmega, as an aside: `Start-Process`, due to using the ShellExecute WinAPI function by default, consults the registry for additional executable locations (beyond `$env:PATH`), so `Start-Process chrome.exe` should suffice. – mklement0 Apr 27 '22 at 22:15
  • I greatly appreciate your willingness to help, and the time you spent with your reply & comments. After more testing on my side, I found out that if I run the `.ps1` file through content menu, by choosing _"Run with PowerShell"_ menu option, it executes the script correctly (i.e., opens the PowerShell window, runs the script, opens Chrome with desired URL, and closes the PowerShell window). However, if I execute the scripts by enter or double-click, the script is executed halfway. It opens the PowerShell window, runs 1st part of the code, does not open Chrome, and closes the PowerShell window. – Ωmega Apr 28 '22 at 14:47
  • I'm glad to hear it, @Ωmega. Without more details, it's hard to know what's going on. If you follow the first link in the answer, you'll find a way to configure double-clicking to keep the session alive and therefore the window _open_ after script execution. For ad hoc troubleshooting you could put a `pause` statement at the end of your script. – mklement0 Apr 28 '22 at 14:52
  • It is important to close the PowerShell window after the script is completed, so I can then configure the shortcut to run the script in a minimized window. – Ωmega Apr 28 '22 at 14:56
  • @Ωmega, understood. Then I suggest using what I proposed purely for troubleshooting; you can then revert to auto-closing. But I'm confused now: if you're running _via a shortcut_, why does double-click behavior matter? – mklement0 Apr 28 '22 at 14:59
  • Shortcut and double-click behaviors are the same. It executes the script properly only when I use the content menu option _"Run with PowerShell"_. I am afraid there is something wrong with Windows 11 configuration, as on Windows 10 machine I experience no such issues. I am admin on both machines, so there is no access/right problem. – Ωmega Apr 28 '22 at 15:03
  • Mysterious, @Ωmega. Perhaps you can copy the command line that the "Run with PowerShell" shortcut-menu command uses into your shortcut file to see if it works then. – mklement0 Apr 28 '22 at 15:54
0

What is the output of running next command in Powershell?

Get-ExecutionPolicy

If the output you get is "Restricted", you can try creating a .cmd file situated in the same folder as the .ps1 file with the next content:

PowerShell.exe -ExecutionPolicy Bypass -File "your_ps1_name.ps1"
jerkdavi
  • 47
  • 2
  • 9