How to get Microsoft Powershell on Linux

PowerShell is Microsoft’s answer to the Unix command-line. It’s powerful and primarily targeted at developers looking for a rock solid command-line and scripting framework.

The PowerShell tool and framework isn’t exclusive to Windows users. To the surprise of many, Microsoft has made it possible to use their PowerShell platform on other operating systems, including Linux.

While a majority of Linux users focus on the Unix command-line that all distributions have to offer, it’s nice to see Microsoft putting Windows-related tools, such as PowerShell on the platform, so that Linux users who need to use Powershell don’t need to use Microsoft’s operating system. Here’s how to get Powershell on Linux.

Install PowerShell on Linux

On Linux, Microsoft has given Linux users many different ways to enjoy PowerShell. As of now, there’s a DEB package for Ubuntu and Debian, an RPM for Fedora, SUSE, CentOS (and other RedHat OSes,) an unofficial AUR package and even a Snap available for installation.

To get PowerShell working on your Linux PC, open up a terminal window by pressing Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. Then, follow along with the instructions that match the Linux distribution you are currently using.

Ubuntu

It’s true that since Ubuntu comes with Snap by default, users should probably install the Snap release instead of fussing with the PowerShell DEB release. However, not every Ubuntu user likes to use Snaps, so it’s nice to see there’s an alternative way to install the software.

To get your hands on the PowerShell Debian package for your Ubuntu PC, use the wget downloader tool.

18.04 LTS

wget https://github.com/PowerShell/PowerShell/releases/download/v6.2.0/powershell_6.2.0-1.ubuntu.18.04_amd64.deb

16.04 LTS

wget https://github.com/PowerShell/PowerShell/releases/download/v6.2.0/powershell_6.2.0-1.ubuntu.16.04_amd64.deb

With the package file is done downloading, it’s time to install it to Ubuntu. To do this, use the dpkg command.

sudo dpkg -i powershell_*_amd64.deb

Following the installation of the PowerShell DEB package on Ubuntu, you may see errors that appear in the command-line prompt. Don’t worry; these errors are likely just dependency issues. To fix them, use the apt install command below.

sudo apt install -f

Assuming apt install runs successfully, you’ll be able to access PowerShell on Ubuntu!

Debian

Microsoft does support Debian users with a PowerShell DEB package, however officially, they’ve only released one for Debian 9 Stable. It’s okay, as most users aren’t on 10 yet, but if you do use 10, skip these instructions in favor of the Snap method instead.

To get PowerShell working on Debian 9, launch a terminal window. From there, run the following wget download command to grab the latest DEB release of the app.

wget https://github.com/PowerShell/PowerShell/releases/download/v6.2.0/powershell_6.2.0-1.debian.9_amd64.deb

When the DEB package is done downloading to your Debian PC, it’s time to start the installation. To do this, run dpkg.

sudo dpkg -i powershell_*_amd64.deb

After running the dpkg command, Debian may experience dependency issues. To fix this, you can run apt-get install.

sudo apt-get install -f

Once your dependency issues are corrected (if there were any), PowerShell will be ready to use!

Arch Linux

Microsoft’s PowerShell is on the Arch Linux AUR, so if you’re looking to install it on your Arch PC, you’re in luck. To start the installation, open up a terminal and use the Pacman package to download Git and Base-devel to the system.

sudo pacman -S git base-devel

Following Git and Base-devel, clone the latest Trizen AUR snapshot, to make installing PowerShell easy.

git clone https://aur.archlinux.org/trizen.git

Install Trizen to the system using makepkg.

cd trizen
makepkg -sri

Finally, install the latest release of Microsoft PowerShell on Arch Linux.

trizen -S powershell

Fedora

Microsoft has released an RPM package for both RedHat Enterprise Linux 7, and Fedora which can be used to install PowerShell. To get your hands on this RPM package file, use wget to download it. Then, install with Dnf.

wget https://github.com/PowerShell/PowerShell/releases/download/v6.2.0/powershell-6.2.0-1.rhel.7.x86_64.rpm

sudo dnf install powershell-6.2.0-1.rhel.7.x86_64.rpm

OpenSUSE

There’s an RPM package of PowerShell ready to install on OpenSUSE Leap 42.3, so those on SUSE are in luck. However, if you’re using 15.0 or Tumbleweed, consider the Snap instructions instead.

To install PowerShell on SUSE 42.3, start by using the wget tool to download the latest RPM file.

wget https://github.com/PowerShell/PowerShell/releases/download/v6.2.0/powershell-6.2.0-1.rhel.7.x86_64.rpm

Once the download is complete, use Zypper to install the package.

sudo zypper install powershell-6.2.0-1.rhel.7.x86_64.rpm

Snap

Aside from generating several packages for various Linux distributions, Microsoft has also chosen to upload PowerShell to the Snap package store. So, if you’re running a Linux distribution that supports Snaps, you’re in luck!

To install the Snap release of PowerShell, follow this guide to learn how to set up Snapd on your system. Then, install the latest release of Microsoft PowerShell with the snap install command below.

sudo snap install powershell --classic

Access PowerShell

To access Microsoft PowerShell on Linux, fire up your favorite Linux terminal application. Once the app is open, run the command below.

pwsh

For help with PowerShell, run:

pwsh help

Read How to get Microsoft Powershell on Linux by Derrik Diener on AddictiveTips – Tech tips to make you smarter

How to run PowerShell scripts with keyboard shortcuts on Windows 10

PowerShell scripts take a little setting up if you want them to run automatically at a given time. It takes more than using just a scheduled task as the script won’t run in PowerShell as an action. If you’re not looking to automatically run these scripts and instead are looking to run PowerShell scripts with keyboard shortcuts, you can do just that. There are some limitations to this process but nothing that makes the process tedious to use.

PowerShell scripts with keyboard shortcuts

The first thing you need is the PowerShell script. Go ahead and create it. Move it somewhere you know you won’t delete it by accident. Once you’ve done that, go to your desktop and right-click on an empty area. From the context menu, select New>Shortcut.

In the location field, enter the following and make sure you replace “path-to-script” with the complete path to the PowerShell script you want to run with a keyboard shortcut.

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "path-to-script"

Example

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\Users\fatiw\Desktop\toastNotification.ps1"

Click Next, and enter a name for the shortcut. If you want, you can also change the icon that the shortcut uses. By default, it will use the PowerShell icon.

After you’ve created the shortcut, right-click it and select Properties from the context menu. Go to the Shortcut tab, click inside the ‘Shortcut key’ field, and record the keyboard shortcut that you want to use to run the app.

You only need to enter a letter or number. The Ctrl+Alt keys are added by default and will be part of all keyboard shortcuts for running apps/shortcuts to apps.

That should do the trick. You can now run the script with the shortcut that you recorded.

Limitations

I mentioned earlier that this method has limitations, though they’re not very restricting. The limitation is that the shortcut, the one you created, must be placed on the desktop or added to the Start Menu. If you move the shortcut anywhere else, e.g., to a different drive on your PC, or nest it inside a folder, the keyboard shortcut will not be able to run the script.

Keeping the script on your desktop is really the most reliable way to run scripts with a keyboard shortcut. It seems that using the Pin to Start option in the shortcut’s menu doesn’t really do the trick and neither does adding it to the Start Menu folder. It may have to do with the script I tested this out on so go ahead and try it out for yourself. If adding the shortcut to the Start Menu won’t work, you’re going to have to place it on your desktop.

Read How to run PowerShell scripts with keyboard shortcuts on Windows 10 by Fatima Wahab on AddictiveTips – Tech tips to make you smarter

How to fix ‘Unable to find module repositories’ error in PowerShell on Windows 10

PowerShell does a few basic things but there are quite a few modules available for it that you can install and use them to get more out of it. Installing a module in PowerShell is pretty easy however, sometimes when you try to install a module you get the error ‘Unable to find module repositories’ which prevents you from installing it. If you’re getting this error, the fix is pretty easy.

Fix ‘Unable to find module repositories’

Open PowerShell with admin rights and run the following command;

Register-PSRepository -Default

Once the command has run, the problem should be resolved. Run the command to install a module or add a repository and this time, it should work. The command completes within a few seconds.

Other solutions

The ‘Unable to find module repositories’ error appears when PowerShell cannot find the repository that you’re adding, or the module that you’re trying to add since it too comes from a repository. The command re-registers the repository gallery which is why, in most cases, it will fix the problem.

Other reasons this error may appear is if PowerShell is unable to communicate with the repository that you want to add. This could be a problem with your internet. If you’re using a VPN or a Proxy service, disable it and then try installing the module again. It’s worth running the Register command again after disabling the proxy before you attempt to install a module.

If disabling the proxy is not an option, you need to enable the proxy for PowerShell as well. If you’re using a proxy via a desktop app, it is highly likely PowerShell is bypassing it. Once you enable the proxy for PowerShell, run the Register command again and then try installing the module.

You should also make sure that you’re running the latest version of PowerShell. If you’re not running the latest version of Windows 10, your version of PowerShell might not be the latest and that’s why the command might be failing. You should also uninstall older versions of PowerShell.

Lastly, and this really is a last ditch effort, try changing the execution policy and then import or install the module. You should also make sure the module is still available. Try installing a different module to see if it installs and if it does, perhaps the problem is with the other module that you’re trying to install. Some modules might have other, alternate methods for installing them so try those.

Read How to fix ‘Unable to find module repositories’ error in PowerShell on Windows 10 by Fatima Wahab on AddictiveTips – Tech tips to make you smarter

How to save command output to file from Command Prompt and PowerShell on Windows 10

The commands that you execute in PowerShell and Command Prompt can output information. Sometimes, it’s just a few lines e.g., when you check the ping, or it might be a lot of lines e.g. when your local IP address or your external IP. Regardless of how long the output is, sometimes you just need to save it to a file so that you don’t have to fetch the information again and again. Both Command Prompt and PowerShell support copy and paste but you can also save command output to a file when the command is run. Here’s how you can do that in both Command Prompt and PowerShell.

Command output to file – Command Prompt

Open Command Prompt and enter the command that you want to run. Before you tap Enter though, add the following at the end;

> name-of-file.txt

You don’t have to create the text file first. Command Prompt will do that for you. If you enter just the name of the file, it will be created in the same directory that the Command Prompt is currently in. You can save the output to a different location by adding the path to the folder you want to save the output to.

> path-to-folder\name-of-file.txt

If you want to save the output to a file but also view it in Command Prompt, you can do that with this command;

> path-to-folder\name-of-file.txt | type

Command output to file – PowerShell

In PowerShell, you can send command output to a file the same way you can in Command Prompt. Enter the command you want to run and before you tap Enter, add the following at the end. Again, you won’t have to create the text file. PowerShell will do that for you.

> name-of-file.txt

This will save the output file to same directory that PowerShell is currently in. To save the output to a file in a different location, you can specify the path to the folder where you want to save it.

If you want to view the output, you can’t do that at the same time that the file is being created. Once it has been created, run this command and you will be able to view its contents.

Get-Content -Path "path-to-file\file-name.txt"

If you’re wondering whether the output can be saved to a different type of file, the answer is no. Both Command Prompt and PowerShell can only natively write to/create text files and there’s no way to format the output so that when it’s saved to a file, it looks a certain way.

Read How to save command output to file from Command Prompt and PowerShell on Windows 10 by Fatima Wahab on AddictiveTips – Tech tips to make you smarter

How To Get The PowerShell Command History On Windows 10

PowerShell on Windows 10 can give you a history of every command you’ve executed in the current session however, for many users this isn’t enough. They need a history of commands executed across sessions. If you need to get the PowerShell command history, and history for the current session alone doesn’t do the trick, there’s a script and a log file that can help you.

Command History Current Session

If you’re running PowerShell 5, you can get the command history for the current session by running the following command;

Get-History

By default, PowerShell can save up to 50 commands but you can change it to save more. Open PowerShell and right-click the title bar. From the context menu, go to Properties. On the Properties window, go to the Options tab. You will see a Command History section where the default is set to 50. Change it to a higher value.

PowerShell History Log

In order to view the history log, you need to first install the PSReadLine module with the following command;

Install-Module PSReadLine

Next, run the following command and it will return a file location. The file at this location will give you a complete history of every command you’ve run in PowerShell.

(Get-PSReadlineOption).HistorySavePath

Cross-Session PowerShell Command History

This is a bit of a long process but it’s worth it. This solution comes from Intel. First open the following location and check if there is a file named Microsoft.PowerShell_profile.ps1 at this location.

C:\Users\<username>\Documents\WindowsPowerShell

If there’s no file there, open PowerShell and run the following command. It will open your Profile file in Notepad but that file doesn’t exist and PowerShell will tell you as much, and ask if you want to create it. Allow it to create the file.

notepad $Profile

Close the notepad file that opens. In PowerShell, run this command to make sure you’re running PowerShell 5.

$PSVersionTable.PSVersion

Next, set your execution policy to RemoteSigned with the following command.

set-executionpolicy remotesigned

Next, you need to install the PsUrl and PSReadline modules. You can install them with the following command though if you get an error, just install them manually by the process outlined on the linked pages, or try adding the repository as a trusted repository and then running the command again.

install-module PsUrl
install-module PSReadline

Now that those modules have been installed, open this file;

C:\Users\<username>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Paste the following inside this file, and save it.

$HistoryFilePath = Join-Path ([Environment]::GetFolderPath('UserProfile')) .ps_history
Register-EngineEvent PowerShell.Exiting -Action { Get-History | Export-Clixml $HistoryFilePath } | out-null
if (Test-path $HistoryFilePath) { Import-Clixml $HistoryFilePath | Add-History }
# if you don't already have this configured...
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

In order to view command history in PowerShell, you need to run the Get-History command however there is one very crucial step to making sure your history is saved. You CANNOT close PowerShell by clicking the close button. Instead you must always close it with the exit command.

You can use the Up and Down arrow keys to cycle through the previous commands.

Read How To Get The PowerShell Command History On Windows 10 by Fatima Wahab on AddictiveTips – Tech tips to make you smarter