zigford.org/setting-powershell-as-default-on-macos.html
2020-07-21 06:49:32 +10:00

132 lines
5.0 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="main.css" type="text/css" />
<link rel="stylesheet" href="blog.css" type="text/css" />
<link rel="alternate" type="application/rss+xml" title="Subscribe to this page..." href="feed.rss" />
<title>Setting Powershell as default on MacOS</title>
</head><body>
<div id="divbodyholder">
<div class="headerholder"><div class="header">
<div id="title">
<h1 class="nomargin"><a class="ablack" href="http://zigford.org/index.html">zigford.org</a></h1>
<div id="description"><a href="about.html">About</a><a href="links.html"> | Links</a><a href="scripts.html"> | Scripts</a><br>Sharing linux/windows scripts and tips</br></div>
</div></div></div>
<div id="divbody"><div class="content">
<!-- entry begin -->
<h3><a class="ablack" href="setting-powershell-as-default-on-macos.html">
Setting Powershell as default on MacOS
</a></h3>
<!-- bashblog_timestamp: #201812180950.22# -->
<div class="subtitle">December 18, 2018 &mdash;
Jesse Harris
</div>
<!-- text begin -->
<p>When you click on 'Terminal.app' on a stock MacOS system, your connected to
the systems pseudo TTY which in turn launches your users default shell.</p>
<p>Terminal.app can be told to launch a process other than your users default
shell (eg, powershell), but there are a few cases where if you might want
to replace your shell system-wide. (eg, sudo can use powershell then)</p>
<hr />
<h3>Step 1</h3>
<p>To do this, you first need to add powershell as an available system shell.
This is done by adding the path to the powershell binary into <code>/etc/shells</code>.</p>
<p>From bash you can do this:</p>
<pre><code> $ which pwsh | sudo tee -a /etc/shells
</code></pre>
<p>Or from an elevated pwsh:</p>
<pre><code> PS&gt; (Get-Command pwsh).Source | Add-Content /etc/shell
</code></pre>
<p>The end result is that your /etc/shells file should look something like this:</p>
<pre><code> $ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/microsoft/powershell/6/pwsh
</code></pre>
<h3>Step 2</h3>
<p>The next step is to set your accounts shell. Do this interactivly by running
<code>chsh</code> or <code>chpass</code> (they are both the same binary). You can set the <code>Shell:</code>
parameter to the path to pwsh. You could also do this non-interactivly in
bash:</p>
<pre><code> $ chsh -s `which pwsh`
</code></pre>
<p>Or via powershell</p>
<pre><code> PS&gt; chsh -s "$((Get-Command pwsh).Source)"
</code></pre>
<h3>Step 3</h3>
<p>Powershell is now the default shell, but we are not finished yet. When bash
is launched on macOS, the first thing executed is /etc/profile. In the
profile <code>/usr/libexec/path_helper</code> is executed which sets up the PATH
environment variable. Powershell doesn't do this so we need to setup a quick
function in our powershell profile to do a similar process.</p>
<h4>Create a powershell profile</h4>
<pre><code> PS&gt; New-Item -ItemType Directory -Path (Split-Path -Path $profile -Parent) -force
PS&gt; New-Item -ItemType File -Path $profile
</code></pre>
<h4>Contents of profile</h4>
<pre><code> function Get-Path {
[CmdLetBinding()]
Param()
$PathFiles = @()
$PathFiles += '/etc/paths'
$PathFiles = Get-ChildItem -Path /private/etc/paths.d | Select-Object -Expand FullName
$PathFiles | ForEach-Object {
Get-Content -Path $PSItem | ForEach-Object {
$_
}
}
$Paths
}
function Add-Path {
Param($Path)
$env:PATH = "${env:PATH}:$Path"
}
function Update-Environment{
[CmdLetBinding()]
Param()
$Paths = $env:PATH -split ':'
Get-Path | ForEach-Object {
If ($PSItem -notin $Paths) {
Write-Verbose "Adding $PSItem to Path"
Add-Path -Path $PSItem
}
}
}
Update-Environment
</code></pre>
<h2>Warning</h2>
<p>Although I've been running with this configuration for a few months, I'm not
sure doing this is excatly advisable. Some apps may be written to
assume that bash is the default on MacOS. VSCode for example may give some
greif.</p>
<p>Your mileage may vary.</p>
<p>Tags: <a href='tag_powershell.html'>powershell</a>, <a href='tag_macos.html'>macos</a></p>
<!-- text end -->
<!-- entry end -->
</div>
<div id="footer">&copy <a href="http://twitter.com/zigford_org">Jesse Harris</a> &mdash; <a href="mailto:jesse&#64;zigford&#46;org">jesse&#64;zigford&#46;org</a><br/>
Generated with <a href="https://github.com/cfenollosa/bashblog">bashblog</a>, a single bash script to easily create blogs like this one</div>
</div></div>
</body></html>