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

0

2 Answers 2

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.

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

8 Comments

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

1 Comment

> Get-ExecutionPolicy says RemoteSigned

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.