Clicky

20230225

Click to run Powershell script

Sometimes it is more convenient to start the Powershell script by just clicking the filename in Windows Explorer. 

Here are a couple of methods to start a Powershell script by clicking the filename or just typing the script name in a command shell.

Method 1: the Shortcut

Create a new shortcut on the Windows desktop or in some folder with the next properties:

  • Create a shortcut on the desktop or in a folder with the next "Target" value:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile "%1"
  • Associate PS1 files with this shortcut.

It will run the Powershell script but the console will be closed on completion. You may want to add a Start-Sleep or Read-Key command at the end of the script to observe the script output.


 

Method 2: using a Batch file

  • Create a Batch file (c:\Scripts\psRun.bat) with the next content:
@cmd /k powershell -noprofile "%1"

  •  Associate PS1 files with this Batch file.

Now the script runs and leaves the Console open, so the script output can be inspected.


Method 3: embed Powershell code in Batch file

A Batch file will run when clicked. So when we embed the Powershell code in a Batch file we can run the Powershell script from the Batch file. In this case we do not have to change the file association of PS1 files.
 
Running this Batch file:

@echo off
set outFile=%temp%\temp.ps1
del /q %outFile% >nul 2>nul
certutil -decode %0 %outFile% >nul
cmd /k powershell -noprofile -file %outFile%
goto :EOF
 
-----BEGIN CERTIFICATE-----
d3JpdGUtaG9zdCAiKioqIEhlbGxvIHdvcmxkIg0KU2xlZXAgMw==
-----END CERTIFICATE-----

Will give this output:

So how does it work? The Powerschell script is converted to Base64. The Windows utility CERTUTIL is used to encode the Powershell scrip to Base64 and all the script will be between the BEGIN/END certificate markers. This is used by CERTUTIL to recognize the code that needs to be translated back to Powershell code.

So the manual steps to create a script like this is:

1. Use the Batch file decoder code

@echo off
set outFile=%temp%\temp.ps1
del /q %outFile% >nul 2>nul
certutil -decode %0 %outFile% >nul
cmd /k powershell -noprofile -file %outFile%
goto :EOF  

2. Convert the Powershell script to Base64

C:\> certutil -encode HelloWorld.ps1 HelloWorld.ps1.b64

 3. Append the HelloWorld.ps1.b64 output to the decoder code and save it as helloWorld.bat

 

To automate the whole process you may use the next script. Save as: makeBatchFromPowershell.bat:

@echo off

REM --- Usage: %0 {filenam}.PS1

REM --- Variables...
    if %1.==. goto :EOF
    if not exist %1 goto :EOF
    set batchFileName=%1-.bat
    set psCodeBase64=%temp%\%random%-%random%.tmp

REM --- Encode the Powershell file in CERTUTIL's Base64 code...
    certutil -encode %1 %psCodeBase64% >nul
    if %errorlevel% NEQ 0 (
        echo *** Certutil error...
        goto :EOF
    )    

REM --- Part 1: Create batch file - decoder section...
    echo @echo off>%batchFileName%
    echo set outFile=%%temp%%\temp.ps1>>%batchFileName%
    echo del /q %%outFile%% ^>nul 2^>nul >>%batchFileName%
    echo certutil -decode %%0 %%outFile%% ^>nul >>%batchFileName%
    echo cmd /k powershell -noprofile -file %%outFile%% >>%batchFileName%
    echo goto :EOF >>%batchFileName%
    echo. >>%batchFileName%

REM --- Part 2: Append Base64 code to batch file...
    type "%psCodeBase64%" >>%batchFileName%
    del /q %psCodeBase64% >nul

REM --- Show resulting filename...
    echo *** %1 embedded into %batchFileName%

 

Tying it all together:


You may rename the helloWorld.ps1-.bat to whatever BAT filename you want.


Real Time Web Analytics