Skip to content

Instantly share code, notes, and snippets.

@fridtjof
Last active January 14, 2024 22:04
Show Gist options
  • Save fridtjof/8499886a9ebd2a5be003b91a769b2de5 to your computer and use it in GitHub Desktop.
Save fridtjof/8499886a9ebd2a5be003b91a769b2de5 to your computer and use it in GitHub Desktop.
This is a wrapper for launching powershell scripts using shebang on WSL. Put the name of your user's directory into the second line, save this file into /usr/bin and make it executable.
#!/bin/bash
YOUR_WINDOWS_USERDIRECTORY_NAME_HERE=""
WIN_TEMP_PATH="/mnt/c/Users/$YOUR_WINDOWS_USERDIRECTORY_NAME_HERE/AppData/Local/Temp"
CURRENT_DIR=`pwd`
if [ $# -eq 0 ]
then
echo "No arguments supplied"
exit 1
fi
SCRIPT_PATH="$1"
if ! [[ "$SCRIPT_PATH" == \/* ]] ;
then
# relative path -> absolute path
SCRIPT_PATH="$CURRENT_DIR/$SCRIPT_PATH"
fi
FILENAME="`basename $SCRIPT_PATH`"
if ! [[ "$SCRIPT_PATH" == \/mnt\/c\/* ]] ;
then
# move to %TEMP% where powershell can use it
cp "$SCRIPT_PATH" "$WIN_TEMP_PATH/"
SCRIPT_PATH="$WIN_TEMP_PATH/$FILENAME"
# add .ps1 if necessary - this only happens when we run a script from WSL
# when we have files in windows land, they should have a ps1 extension anyway because windows likes its extensions
if ! [[ "$FILENAME" == *\.ps1 ]] ;
then
mv "$SCRIPT_PATH" "$SCRIPT_PATH.ps1"
SCRIPT_PATH="$SCRIPT_PATH.ps1"
fi
fi
# substitute /mnt/c/ with C://
SCRIPT_PATH=${SCRIPT_PATH/\/mnt\/c/C\:\/\/}
/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -File "$SCRIPT_PATH" ${@:2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment