如何在macOS安装PowerShell及将其集成至系统终端?
Hey there! I get that you've already installed PowerShell using the PKG installer and can launch it from your Applications folder, but want the convenience of accessing it directly from your macOS terminal. Let's walk through how to fix this—chances are it's just a quick path setup or command clarification issue.
First, let's clear up a common confusion: on macOS, the official command to launch PowerShell after PKG installation is pwsh, not powershell. That's probably why you weren't seeing the expected behavior when trying the latter!
Here's the step-by-step fix:
1. Verify the PowerShell executable path
First, confirm the location of the PowerShell binary inside the app bundle. Open your terminal and run:
ls /Applications/PowerShell.app/Contents/MacOS/pwsh
If you see the file listed, you're good to go. If not, double-check that the PowerShell app is actually in your Applications folder.
2. Create a symbolic link to make it accessible system-wide
The easiest way to make pwsh available from any terminal window is to create a symbolic link from the app's binary to a directory that's already in your system's PATH (like /usr/local/bin, which is standard for user-installed tools).
Run this command in your terminal (you'll need to enter your admin password when prompted):
sudo ln -s /Applications/PowerShell.app/Contents/MacOS/pwsh /usr/local/bin/pwsh
If you get an error saying /usr/local/bin doesn't exist, create it first with:
sudo mkdir -p /usr/local/bin
Then re-run the symbolic link command.
3. Test it out
Close your terminal window and open a new one. Now just type:
pwsh
You should see the PowerShell prompt (usually starting with PS) pop up, meaning you're now in the PowerShell environment directly from your macOS terminal!
Troubleshooting if it still doesn't work
- Check if
/usr/local/binis in yourPATHby runningecho $PATH. If you don't see it listed, you'll need to add it to your shell's configuration file:- For Zsh (default on newer macOS versions): Open
~/.zshrcin a text editor and add this line:
Save the file, then runexport PATH="/usr/local/bin:$PATH"source ~/.zshrcto apply the changes immediately. - For Bash: Edit
~/.bash_profileor~/.bashrcinstead, using the sameexport PATHline, then source the file.
- For Zsh (default on newer macOS versions): Open
That's all there is to it—you can now switch between macOS terminal and PowerShell with just the pwsh command, no need to navigate to the Applications folder anymore!
内容的提问来源于stack exchange,提问作者SuicideSheep

