Change default Login Shell to PowerShell on Linux

With PowerShell 7, PowerShell Core has been improved and resolved the issues with environmental variables in the *-nix based operating systems. If you have been working with Linux Servers and are using PowerShell to perform some kind of administrative tasks, you may also want to set it as default login shell for certain users. This makes it easy to invoke certain scripts since a familiar shell is available when you are calling the environment.

There are many ways to change the shell like using usermod or chsh or may be if you are feeling adventurous, by modifying /etc/passwd directly using vi editor. So let’s get started.

Install PowerShell 7 on Linux

The sheer number of distributions and their different releases available in Linux, makes it harder to list tasks for all of them. You can use this official doc to get steps to install PowerShell 7 suitable for your distribution. For the purpose of this blog, we’ll be installing PowerShell 7 on the Ubuntu Server 20.04 LTS.

Once its installed, lets verify the version by using $PSVersionTable inside pwsh shell:

Verify PowerShell is available as Shell

We can do this by simply output the contents of /etc/shells:

Create a new User in Linux

This step is not necessary. You can choose to modify login shell for already existing users, which we’ll discuss next. We can create a new user by using useradd command:

user_root@f46d9194c11c:~$ sudo useradd pscore
user_root@f46d9194c11c:~$ sudo passwd pscore
 New password: 
 Retype new password: 
 passwd: password updated successfully

We can verify the default login shell for new user by first switching to it and then output value of environmental variable $SHELL:

user_root@f46d9194c11c:~$ su pscore
 Password: 
 $ 
 $ whoami
 pscore
 $ $SHELL
 $ echo $SHELL
 /bin/sh

Modify the Login Shell to PowerShell

We can modify the login shell by using usermod or chsh command as shown below:

usermod --shell /usr/bin/pwsh pscore
chsh --shell /usr/bin/pwsh pscore

Once its done, lets switch to user we created and verify our changes:

Note that to access environmental variable $SHELL inside PowerShell, we have to use PowerShell specific syntax now, as shown above.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s