Skip to content

Instantly share code, notes, and snippets.

@quonic
Last active October 8, 2023 01:43
Show Gist options
  • Save quonic/69891687716332eb81b76ea0d17e91c3 to your computer and use it in GitHub Desktop.
Save quonic/69891687716332eb81b76ea0d17e91c3 to your computer and use it in GitHub Desktop.
Install PowerShell 7 on Ubuntu (23.04/22.04/20.04/18.04), Debian (12/11/10/9) Fedora/RHEL(8/7), Alpine, or macOS(Intel/M1)
#!/usr/bin/env bash
# Check if user is root
if [ "$(id -u)" -ne 0 ]; then
echo "Please run as root"
exit
fi
Use_Package_Manager=0
Help_Message=0
POSITIONAL=()
while (($# > 0)); do
case "${1}" in
-p | --package-manager)
echo "Using package manager"
Use_Package_Manager=1
shift
;;
-h | --help)
echo "Usage:"
echo " -p|--package-manager Use the systems package manager."
echo " When Microsoft's repo's are already"
echo " added to the system's package manager."
echo " Note: On macOS, this will attempt to"
echo " install brew if not found."
echo " -h|--help Displays usage help info."
Help_Message=1
shift
;;
*)
echo "Usage:"
echo " -p|--package-manager Use the systems package manager."
echo " When Microsoft's repo's are already"
echo " added to the system's package manager."
echo " Note: On macOS, this will attempt to"
echo " install brew if not found."
echo " -h|--help Displays usage help info."
Help_Message=1
shift
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional params
if ((Help_Message == 1)); then
exit 0
fi
if ((Use_Package_Manager == 1)); then
if [[ $OSTYPE == 'darwin'* ]]; then
if [ "$(command -v brew)" ]; then
echo "Command \"brew\" exists on system"
else
echo "Command \"brew\" does not exists on system, installing..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
echo "Installing powershell via brew"
brew install --cask powershell && exit 0 || exit 1
else
echo "Installing powershell via systems's package manager"
if [[ $(which dnf) ]]; then
echo "Using dnf"
dnf install powershell
elif [[ $(which yum) ]]; then
echo "Using yum"
yum install powershell
elif [[ $(which apt) ]]; then
echo "Using apt"
apt -y install powershell
fi
fi
fi
download_latest_release() {
if [[ $OSTYPE == 'darwin'* ]]; then
if [[ $(uname -m) == 'arm64' ]]; then
# M1
PKG="powershell-*-osx-arm64.pkg"
else
# Intel
PKG="powershell-*-osx-x64.pkg"
fi
else
if [[ $(which apk) ]]; then
PKG="powershell-*-linux-alpine-x64.tar.gz"
else
PKG="powershell-*-linux-x64.tar.gz"
fi
fi
curl -s https://api.github.com/repos/PowerShell/PowerShell/releases/latest | grep "${PKG}" | cut -d : -f 2,3 | tr -d \" | wget -qi -
}
# Install pwsh
if [ "$(command -v pwsh)" ]; then
echo "PowerShell already installed."
exit 0
else
if [[ $OSTYPE == 'darwin'* ]]; then
# macOS
download_latest_release
xattr -rd com.apple.quarantine "$(ls powershell-*-osx-*.pkg)"
installer -pkg "$(ls powershell-*-osx-*.pkg)" -target /
else
# Linux
if [[ $(which yum) ]]; then
majversion=$(lsb_release -rs | cut -f1 -d.)
fedoraversion=$(rpm -q --queryformat '%{VERSION}\n' fedora-release)
if [ "$majversion" -eq "7" ]; then
yum install -y https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
elif [ "$majversion" -eq "8" ]; then
yum install -y https://packages.microsoft.com/config/rhel/8/packages-microsoft-prod.rpm
elif [ "$fedoraversion" -ge "31" ]; then
rpm --import https://packages.microsoft.com/keys/microsoft.asc
curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
dnf check-update
dnf install compat-openssl10
dnf install -y powershell
fi
elif [[ $(which apt) ]]; then
if [ -f /etc/debian_version ]; then
IFS='.' read -r -a VERSION </etc/debian_version
v=${VERSION[0]}
if [[ ${v} == 9 ]]; then
apt update -y
apt install -y curl gnupg apt-transport-https
wget https://packages.microsoft.com/config/debian/9/packages-microsoft-prod.deb || exit 1
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - || exit 1
sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/microsoft.list'
else
wget "https://packages.microsoft.com/config/debian/$v/packages-microsoft-prod.deb"
sudo dpkg -i packages-microsoft-prod.deb
fi
apt update -y
apt install -y powershell
else
r=$(lsb_release -rs)
apt update -y
apt install -y wget apt-transport-https software-properties-common
wget -q "https://packages.microsoft.com/config/ubuntu/$r/packages-microsoft-prod.deb" || exit 1
dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
apt update -y
apt install -y powershell
fi
elif [[ $(which apk) ]]; then
apk add --no-cache ca-certificates less ncurses-terminfo-base krb5-libs \
libgcc libintl libssl1.1 libstdc++ tzdata userspace-rcu zlib icu-libs \
curl
apk -X https://dl-cdn.alpinelinux.org/alpine/edge/main add --no-cache lttng-ust
cd /tmp/ || exit 1
download_latest_release
mkdir -p /opt/microsoft/powershell/7
tar zxf "/tmp/$(ls -A1 powershell-*-linux-alpine-x64.tar.gz)" -C /opt/microsoft/powershell/7
chmod +x /opt/microsoft/powershell/7/pwsh
ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh
else
echo "Unknown/Unsupported OS. Please install PowerShell before running this script."
exit 1
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment