robertspierre
robertspierre

Reputation: 4430

Open a file with a PowerShell script in explorer

I wanted to use Windows built-in table viewer to open CSV file, like this SO answer shows.

So I want that when I double click on a CSV file in Explorer, the following command is run:

Import-Csv [CSV-FILE] |Out-GridView

I ended up creating two script files. One file name "read_csv.bat" which contains:

powershell.exe -ExecutionPolicy ByPass -noexit -File %~dp0read_csv.ps1 -csvfile %1
pause

Another file name "read_csv.ps1" which contains the actual script

param (
    [Parameter(Mandatory=$true)][string]$csvfile
 )

Import-Csv $csvfile |Out-GridView

Is there now way to do it more efficiently, so with only one script file? If I set explorer to open the CSV file with the POwerShell script directly, a blue message appears

This app can't run on your PC To find a version for your PC, check with your published

Upvotes: 2

Views: 1146

Answers (1)

mklement0
mklement0

Reputation: 440112

Note:

  • Since powershell.exe is ultimately called, a console window will invariably (also) open when a CSV file is opened, because powershell.exe is a console-subsystem application.

  • Providing an alternative, GUI-subsystem executable to avoid creating a console window is the subject of this feature request on GitHub; in the meantime, there are workarounds:

    • A VBScript-based solution is shown in this answer.

    • A script-less, but complex and potentially AV-software-triggering alternative is used in this answer.


You'll need to use an adapted version of the powershell.exe command from your batch file, because *.ps1 files are by design not directly executable.

That is, in the registry definition for the CSV file type, use the following command to define the action for the Open verb (see below):

powershell.exe -ExecutionPolicy ByPass -NoExit -File c:\path\to\read_csv.ps1 -csvfile "%1"

Be sure to substitute the real path to your *.ps1 script for c:\path\to\read_csv.ps1 (double-quote it, if necessary); you can either use a literal path, or one based on cmd.exe-style environment-variable references (e.g., "%USERPROFILE%\read_csv.ps1").

Alternatively, you can make do without a script file altogether, using the PowerShell CLI's
-Command parameter
:

powershell.exe -ExecutionPolicy ByPass -NoExit -Command Import-Csv \"%1\" | Out-GridView

To automate the process of configuring this command for opening CSV files via File Explorer / the desktop:

The code below modifies the registry as follows:

  • defines a new file type, PsCsvViewer, with an Open verb (operation) that invokes the PowerShell command defined above.

  • associates the .csv filename extension with that new file type.

    • Note: The first time you open a CSV file after the redefinition, you may be prompted to confirm the intent to use a PowerShell command from now on.
  • creates the definitions above for the current user only, which means that you don't need admin privileges to run the code (which writing to subkeys of HKEY_CLASSES_ROOT\ would require).

# Note the need for *3* "\", because an extra layer of escaping is
# needed for reg.exe.
$cmd = 'powershell.exe -ExecutionPolicy ByPass -NoExit -Command Import-Csv \\\"%1\\\" | Out-GridView'

# Create a new file type for the PowerShell command.
reg.exe add HKCU\Software\Classes\PsCsvViewer\Shell\Open\command /ve /d $cmd  /f

# Associate .csv files with the new file type.
reg.exe add HKCU\Software\Classes\.csv /ve /d PsCsvViewer /f

Upvotes: 1

Related Questions