I recently started working at a company that has extensive Windows based tooling and software stack.
As I had previously been working at companies that had mainly Linux and Unix based software stacks, this was an interesting change and challenge to learn new platforms and technologies.
For example, have you ever tried to automate the creation of an extended Active Directory Domain and Schema using LDIF files? Funny story, when working a lot with LDIF files I was frustrated that I could not find a syntax highlighter for Visual Studio Code, my favorite IDE, so I decided to just write my own 😉
Haven’t heard of Visual Studio Code? it’s a lightweight IDE based on Electron, cross platform, open source, with a vibrant community and a built-in marketplace that has a plethora of extensions for almost any language and need imaginable to a Developer, but this is a subject for another post 🙂
Since as a DevOps Engineer, you always need to make use of the command line. It can be both for ad-hoc commands or for writing comprehensive automation scripts. Whenever possible, I tried to use cross platform utilities such as Python or the relatively new Bash shell on Windows 10 feature. However, for some existing Windows based builds and tasks I had no choice but to adapt my familiar shell commands from Linux to Powershell.
Fortunately, Powershell itself includes nifty aliases to native Powershell cmdlets that fulfill the same expected functionality like their Linux shell counterparts.
Some examples include:
cd -> Set-Location
ls -> Get-ChildItem
cp -> Copy-Item
mv -> Move-Item
cat -> Get-Content
pwd -> Get-Location
mkdir -> New-Item -ItemType Directory
You got the gist.
However, not all commands have these convenient aliases for Linux users and some of the basic commands that you have been using in Linux shells simply do not exist.
Here is a few of my top Powershell equivalents for Linux commands.
1. Select-String =>
This cmdlet is the
grep command equivalent for searching and extraction of text patterns from text files.
It’s worth mentioning about
- Default mode is case insensitive (Like Windows itself).
- You can skip piping to
-Replaceflag comes out-of-the-box for this cmdlets, like in many others.
- It’s got a nifty short alias:
2. Write-Output =>
When all you want is to simply echo some text out to the terminal.
It’s worth mentioning about
- it’s different from the
Write-Hostcmdlet. The output is sent to “stdout” as opposed to the console itself, which makes it easy to pipe to subsequent commands, you can read more about this in this answer.
3. Get-Childitem =>
This command is not exactly a replacement but more like a mixture of
find since it includes file information as well as finding the files themselves by filename.
For example the default output of the command on Linux
find . -name "*cont*"
yoseft@YOSEF-LAPTOP:~$ find . -name "*cont*" ./.local/lib/python2.7/site-packages/ansible/galaxy/data/container_enabled ./.local/lib/python2.7/site-packages/ansible/galaxy/data/container_enabled/meta/container.yml.j2 ./.local/lib/python2.7/site-packages/ansible/modules/cloud/docker/docker_container.py ./.local/lib/python2.7/site-packages/ansible/modules/cloud/docker/docker_container.pyc ./.local/lib/python2.7/site-packages/ansible/modules/cloud/lxc/lxc_container.py
Is in Powershell:
PS C:\WINDOWS\system32> Get-ChildItem . "*cont*" Directory: C:\WINDOWS\system32 Mode LastWriteTime Length Name ---- ------------- ------ ---- d---s- 10/15/2017 10:21 PM containers d----- 9/29/2017 5:41 PM MailContactsCalendarSync -a---- 9/29/2017 4:41 PM 518 @WindowsUpdateToastIcon.contrast-black.png -a---- 9/29/2017 4:41 PM 810 @WindowsUpdateToastIcon.contrast-white.png -a---- 9/29/2017 4:41 PM 920064 AppContracts.dll
As you can see, we get additional file information about the desired files.
4. Get-Date =>
Pretty straightforward 🙂
5. Get-Command =>
This will give you basic info about the command, function, or alias given as a parameter. Although typically it will give you the type of command, name, version and source module but not the location of the executable on the filesystem like in Linux.
PS C:\WINDOWS\system32> Get-Command get-help CommandType Name Version Source ----------- ---- ------- ------ Cmdlet Get-Help 18.104.22.168 Microsoft.PowerShell.Core
These are some of the commands I have found useful.
What are your favorite Linux command equivalents in Powershell?