Powershell Tips for Linux Users
I’m an avid enjoyer the Linux Terminal experience. Whether it’s for autocompleting my git commands or tailing a log file, it’s just a great way to continue working on your computer without taking your hands off your keyboard.
Sometimes I have to work with Powershell which can be fussy at first but as you get the hang of it and learn to customise it, you can make it a “home away from home” so to say. If you haven’t already, I recommend starting with my post on Windows Terminal with OhMyPosh, it really helps to customise things a bit.
After that, here are some tips for anyone coming from Linux.
Aliases
Not unlike the alias
command in Bash, you can create an alias in Powershell as well. In fact, there are quite a few already there like ls
Viewing Existing Aliases
To view existing aliases, you can use the Get-Alias
cmdlet:
Get-Alias
This command lists all the currently defined aliases in the session. You can also search for specific aliases by providing a pattern:
Get-Alias -Name "ls"
This would show the cmdlet or command that the alias ls
refers to.
Creating New Aliases
To create a new alias, you use the New-Alias
cmdlet:
New-Alias -Name "myalias" -Value "Get-Process"
This command creates a new alias called myalias
that refers to the Get-Process
cmdlet. However, it’s important to note that aliases created with New-Alias
are only available in the current PowerShell session. If you close the session, the alias will be lost.
Making Aliases Persistent
To create aliases that persist across sessions, you can add them to your PowerShell profile script. Here’s how you could add the previous alias to your profile:
- Open your PowerShell profile script for editing. If you’re not sure where it is, you can find its path with
$PROFILE
. - Add the following line to your profile script:
Set-Alias -Name "myalias" -Value "Get-Process"
- Save the script and restart PowerShell. The alias will now be available in all future sessions.
Removing Aliases
To remove an alias, you can use the Remove-Item
cmdlet, specifying the alias path:
Remove-Item alias:myalias
This command removes the alias myalias
from the current session.
Friendly note on aliases
Though they are convenient, their extensive usage in larger scripts is discouraged. They reduce clarity and portability of your scripts, or in some cases could be outright damaging if someone has the wrong alias created.
Rather stick to using the full cmdlet name when writing scripts.
.Net Libraries
Originally Windows PowerShell was a (you guessed it) Windows component built on .Net Framework but in 2016 it was made open-source and cross-platform and dubbed PowerShell Core - built on .Net (previously .Net Core).
Something that’s really cool about that is that you can use .Net libraries in your PowerShell scripts or even just in your commands if you so choose.
Let’s look at using System.IO.Compression.FileSystem
to zip a folder for us:
# Load the necessary .NET assembly
Add-Type -AssemblyName System.IO.Compression.FileSystem
# Define the source directory and the destination ZIP file
$sourceDirectory = "/path/to/your/folder"
$destinationZipFile = "/path/to/your/destination.zip"
# Use the .NET class to create a ZIP file from the source directory
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourceDirectory, $destinationZipFile)
Pretty simple, even though it is likely a roundabout way of creating a zip file.
Piping
That’s right, not unlike Bash, Powershell allows you to pipe the output of one command to another, simply by using the |
character.
A simple example would be reading a list of files into VSCode to enable you to further search through that list.
# First we want to grab all the .txt files in the current directory
Get-ChildItem -Path . -Filter *.txt
# That's simple enough, but let's pipe it to VSCode
Get-ChildItem -Path . -Filter *.txt | code -
Take note of the -
parameter I’m passing to VSCode, that allows it to open content from an external application.
Tailing logs
If you’ve ever had to observe logs in Linux, you’ve probably used tail -f
to open the log and follow the end of the file. This is possible in PowerShell as well using the Get-Content
cmdlet.
Let’s go step-by-step to make sure we don’t miss anything, and I’ll even through in a bonus at the end. First up, the Get-Content
cmdlet can be used as follows:
Get-Content [logs\logfile.txt]
Substitute with your own log file. Simple enough? Log files can be huge though and this will be quite a lot of text. Let’s say we only want the last 10 lines:
Get-Content [logs\logfile.txt] -tail 10
Now let’s say that log file is busy and being written to by a running application. The command on it’s own will only really give you a snapshot of the moment you run it, not a running list. That’s where the wait
parameter comes in handy:
Get-Content [logs\logfile.txt] -tail 10 -wait
Now you’ll have a running tally of the last 10 lines of the file. Really easy if you’re trying to keep an eye on an application to ensure it’s behaving as it should.
Bonus - OGV!
Given the above, if you are searching through a log file, VSCode can only go so far and it’s not always the best at live files that are constantly being updated. But there is actually a tool that you already have (most likely) that can help, called OGV (Out-GridView).
Let’s pipe our above content to it:
Get-Content [logs\logfile.txt] -wait | ogv
It is that simple. I dropped the -tail 10
parameter as Out-GridView is actually great at filtering through logs and sorting through columns, all while it’s updated live.
I hope these helps some people out and gives some people joy from using PowerShell.