Launch .CSX as conveniently as .BAT files

By Beej

first, get Chocolatety if you don’t already have… killer handy

@powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

then make sure you have a .csx runner… either ScriptCS or csi.exe starting with Visual Studio 2015 update 3

choco install ScriptCS

setup the .csx to runner file association

assoc .csx=CSharpScript
ftype csharpscript=C:\ProgramData\chocolatey\bin\scriptcs.exe %1 -- %~2

%~2 means all args starting with #2… see ftype docs for more

FYI for TCC LE folks, apparently i’ve stumbled into a rare backwards compatibility gap… TCC LE doesn’t seem to support the %~2 syntax like cmd.exe and it escapes “%” differently, so substitute that portion of the ftype line above with “%%1 — %%2 %%3 %%4”

lastly, edit your PATHEXT system environment variable to include .CSX which allows us to leave off the extension to launch our scripts just like native commands! =)

setx PATHEXT %PATHEXT%;.CSX /M
refreshenv.cmd

TCC LE requires OPTION > Startup > PathExt for the above to work

refreshenv comes from Chocolately… it refreshes the current command line environment from registry, which we just updated via setx

or runas admin > C:\Windows\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariables

image

with all that fun stuff in place, we can finally do something nifty like this:

C:\> myCSXScript arg1 arg2

beautiful! =)

see my other CSX examples

Tags: CommandLine
Share: Twitter Facebook LinkedIn