image

As the interoperability is increasing day by day, you may need to make the powershell shell to live inside the bash shell.

Thankfully powershell modules can be packaged and are fully compatable in Linux distro.

For Ubuntu you may use this script:

# Update the list of packages
sudo apt-get update
# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common
# Download the Microsoft repository GPG keys
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Update the list of packages after we added packages.microsoft.com
sudo apt-get update
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh

And, that’s it, you can run the powershell commands now from a bash prompt…

PowerShell 7.2.6
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /home/user/userhome>

The powershell comes as an extension, hence you can try some interoperational commands…

Run PoweShell commands with ease

PS /home/user/userhome> Write-Output "This is a powershell command."

Create a multiline Powershell var

PS /home/user/userhome> $iAmPWSHVar=@"
>> I am a
>> powershell var
>> "@
PS /home/user/userhome> $iAmPWSHVar
I am a
powershell var

Now lets try a combo with the hardcore Linux sed replacement with the powershell variable as input, thats fun …

PS /home/user/userhome> $iAmPWSHVar | sed 's/powershell/powershellBash/g'
I am a
powershellBash var

This comes really handy, in cases like when you follow a tutorial in Powershell, but you have to deal only from a bash shell…