I’m finding myself mix and matching between GNU/Linux and Microsoft PowerShell quite a bit at the moment and which most Linuxy/Linuxesque (?) commands work fine under PowerShell, some are a little bit different.
Here’s a few which may be handy to know (the only order is that which I entered them). I’ve also added a couple of Windows commands where they are slightly easier to remember/shorter than PowerShell – and, me being me, included links to the appropriate documentation where possible.
| Linux Command | PowerShell Command | Description |
|---|---|---|
which <commandname>Example: which composer GNU “which” | Get-Command <commandname>Example: Get-Command composer | Find the location of an executable called <commandname>In PowerShell, Get-Command can be referred to as just “ gcm” so the example would become “gcm composer” |
printenv GNU “printenv” | Get-Item -Path Env: | List all environment variables. PowerShell also supports using “ ls” to list environment variables – such as “ls env:“ |
<variablename>="<value>"Example: HELLO="you guys" | Set-Item -Path Env:<variablename> "<value>"Example: Set-Item -Path Env:HELLO "you guys" | Sets an environment variable called <variablename> to the text <value>PowerShell also supports using “ $env:<variablename>="<value>” ” – such as ” $env:HELLO="you guys" “ |
echo $<variablename>Example: echo $PATH GNU “echo” | Get-ChildItem -Path Env:<variablename>Example: Get-ChildItem -Path Env:PATH | Show the value of an environment variable called <variablename>PowerShell also supports using “ ls” to list environment variables – such as “ls env:PATH“ |
echo <text>Example: GNU “echo” | Write-Output <text>Example: Write-Output "Hello there!" | Displays/prints a <text> string to the terminal |
grep "<string>" <file>Example: grep "sort code" document.txt GNU “grep” | Select-String "<string>" <file>Example: Select-String “sort code” document.txt | Search a file <file> for the given text <string> using a regular expression |
grep "<string>" <file>Example: grep "sort code" document.txt GNU “grep” | findstr /R "<string>" <file>Example: findstr /R "sort code" document.txt Windows Command: “findstr” | Search a file <file> for the given text <string> using a regular expression |
ls | grep "<string>"Example: ls | grep "\.html" GNU “ls” | ls | Out-String -Stream | Select-String "<string>"Example: ls | Out-String -Stream | Select-String “\.html” | Search a directory listing for a filename containing <string> using a regular expression |
ls | grep "<string>"Example: ls | grep "\.html" GNU “ls” | dir | findstr /R "<string>"Example: dir | findstr /R "\.html" Windows Command: “findstr” | Search a directory listing for a filename containing <string> using a regular expression. |
wget <url> --output-document <filename>Example: wget https://h.tld/f.gif --output-document o.gif GNU “wget” | Invoke-WebRequest <url> -OutFile <filename>Example: Invoke-WebRequest https://h.tld/f.gif -OutFile o.gif | Save a file at a URL <url> as a local file called <filename> |
\(backslash) Example: a \ b GNU “The Backslash Character” | `b | Allows a command to be split across multiple lines using the multiline separator/line continuation character. |
&&Example: echo "A" && echo "B" GNU “Lists | &&Example: Write-Output "A "&& Write-Output "B" | Command chaining using pipeline operators/list operators – if the condition on the left is true/passes, then continue. |
rm <file>Example: rm test.tmp GNU “rm” | Remove-Item <file>Example: Remove-Item test.tmp | Deletes a file called <file> |
unzip <file>Example: unzip myfile.zip | Expand-Archive <file>Example: Expand-Archive myfile.zip | Extracts a .zip archive file |
| uname Example: uname -nrmo GNU “uname”(note, the output order of uname in GNU/Linux cannot be altered) | Get-ComputerInfoExample:Get-ComputerInfo -Property CsDNSHostName, OsVersion, OsArchitecture, OsName | ConvertTo-Json | Gets basic information about the system. For example: Computer’s “hostname” [GNU:n PS: CsDNSHostName] OS or kernel release [GNU: r PS:OsVersion] Machine/Processor type [GNU:m PS:OsArchitecture] Operating system (OS) name [GNU:o PS: OsName] |
Microsoft PowerShell – don’t you mean Windows PowerShell?
Nope.
If you have a Windows 11, 10, 8.1, 8.0 or 7 machine, it would have come “with default” with Windows PowerShell – however, Microsoft has replaced that with a new “multiplatform” version which – in their infinite wisdom – have decided to call Microsoft PowerShell but haven’t “forcibly upgraded” people (you can upgrade yourself via their “Installing PowerShell on Windows” guide).
Microsoft do have a webpage about the differences between the PowerShells if you want to read: but if you use the command line a lot, it might be worth looking at Windows Terminal as well.
Which version of PowerShell do I have?
If you type/copy:
(Get-Host).Version
into the shell, you’ll get:
- A “Major” version number of 7 or above = (new) Microsoft PowerShell
- A “Major” version number of 5 or lower = (old) Windows PowerShell
- A “Major” version number of 6 = I don’t think this actually exists and therefore should be used as the “cut-off” point between the two.
- “.Version was unexpected at this time” = Probably using Windows Command Prompt
- “-ash: syntax error: unexpected word” = You are in a shell of a Linux machine!
- “bash: syntax error unexpected token `.Version’ = Linux again!
Real Life Example
As an example, here’s a single line Linux “command script” which uses curl to fetch a page from https://api.example/latest , parse it for the first occurrence of the text:
"tag_name":"v..."
(where v…. is any sequence of characters starting with “v” and ending with a quote mark) and store that as $VERSION then pass that to curl again to download that specific version zip file from https://example.com/v${VERSION}.zip as “example.zip” (replacing ${VERSION} with the extracted version number), unzip it and then delete the downloaded .zip file.
And, yes, I’ve colour coded the appropriate sections so you can see which command “maps” to which other command.
Linux Example
VERSION=$(
curl --silent "https://api.example.com/latest" | \
grep '"tag_name":' | \
sed -E 's/.*"v([^"]+)".*/\1/' \
) && curl -L https://example.com/v${VERSION}.zip -o example.zip \
&& unzip example.zip && rm example.zip
Windows PowerShell 7 Example
if (((Invoke-WebRequest "https://api.example.com/latest").Content) -match '"tag_name":"v(?<ver>[^"]+)"') {
$VERSION=$Matches.ver &&
Invoke-WebRequest "https://example.com/v${VERSION}.zip" -OutFile example.zip &&
Expand-Archive example.zip && Remove-Item example.zip }
GNU “which”
Windows Command: “findstr”
One Comment
What about the windows command line CMD (cmd.exe) differences where not using power shell.
Two examples
=
Linux
command=number or other here
windows
command “number or other here”
then there is the minus character which likely catches many people as did me
–
—
–
Linux
–
Windows
–
Notice the size difference but on many web page or other the long Linux minus could be taken as — on windows depending on the font. Why Linux would ever consider making a font like that who knows.
But would be good if you could give more examples maybe a new list that shows the difference between Linux and Windows as many Linux softwares are now Windows but those developers or people that build for Windows leave instructions and files still with Linux conventions but do not convert to what Windows would require.