Being an avid linux user, I am used to the linux commands. But I often work on a windows machine, which is when I look for powershell equivalent of linux commands.
I created this post to keep a track of the commands that I use often.
Well-known linux commands and their powershell equivalents
The PowerShell commands for working with the file system are same as that of linux ones. You navigate around the file system with the cd
(alias for Set-Location
) command.
Files are copied with the copy
or cp
(aliases for Copy-Item
) commands.
Files can be moved with the move
and mv
(aliases for Move-Item
) commands, and removed with the del
or rm
(aliases for Remove-Item
) commands.
You may wonder Why two aliases exist for each command? The answer is simple. One set of names is familiar to cmd.exe/DOS users and the other is familiar to UNIX users. In practice, they’re aliases for the same command, designed to make it easy for people to get going with PowerShell.
Following table lists out some other useful aliases. 1
UNIX command | PowerShell alias | PowerShell cmdlet |
---|---|---|
cd | sl , cd , chdir | Set-Location |
clear | cls clear | Clear-Host |
cp | cpi , cp , copy | Copy-Item |
rm | ri , del , erase , rd , rm , rmdir | Remove-Item |
ls | gci , dir , ls | Get-ChildItem |
echo | write echo | Write-Output |
mkdir | ni | New-Item |
mv | mi , move , mi | Move-Item |
popd | popd | Pop-Location |
pwd | gl , pwd | Get-Location |
pushd | pushd | Push-Location |
mv | rni , ren | Rename-Item |
cat | gc , cat , type | Get-Content |
Note: In PowerShell Core v6 for Linux or macOS, these PowerShell aliases are unavailable. The have been purposefully removed to prevent conflict with native commands on Linux and macOS. The aliases are present only in the Windows versions of PowerShell Core v6.
In the following sections, I will list out some other useful commands.
Powershell equivalent of the linux touch command
The powershell equivalent of the linux touch
command is:
echo $null > <filename>
Note: Here
<filename>
is a placeholder for the name of the file you want to create.
Note: This will overwrite the file if it already exists.
Powershell equivalent of the linux cat command
The powershell equivalent of the linux cat
command is:
Get-Content <filename>
Note: the -raw switch can be used to get the raw content of the file. This is useful when the file contains binary data.
Powershell equivalent of the linux where command
The powershell equivalent of the linux where
command is:
Get-Command <command>
Note: here
<command>
is a placeholder for the command you want to find.
Example
To find the location of the python executable, you can run:
Get-Command python
This will give you a output similar to the following:
CommandType Name Version Source
----------- ---- ------- ------
Application python.exe 3.9.115... C:\Python39\python.exe
Powershell equivalent of copy to clipboard
The powershell equivalent of copying to clipboard is:
<command> | clip
For example, to copy the output of the Get-Command python
command to clipboard, you can run:
Get-Command python | clip
Powershell equivalent of the linux grep command
The powershell equivalent of the linux grep
command is:
Select-String <pattern> <filename>
Note: here
<pattern>
is a placeholder for the pattern you want to search for.<filename>
is a placeholder for the file(s) you want to search in.
The pattern can be a simple string or a regular expression. For more information on regular expressions, see this.
Example
To search for the pattern python
in the file test.txt
, you can run:
Select-String python test.txt
Consider a test.txt
file with the following content:
This is a test file.
This file contains the word python.
The output of the above command will be:
test.txt:2:This file contains the word python.