Powershell tips for the every day developer

Introduction

Some months ago I decided to give Powershell a try and learn to use it for my daily tasks as a developer. I'll share here some common tasks I have to do almost every day and how I accomplish them using Powershell.

Finding files and directories

Who hasn't have the need to find a file in a directory without having any idea where to start looking for it? The Get-ChildItem command can help you with that.

Get-ChildItem without any argument lists all the content of the current directory, but it can do much more than that. For instance, let's say that I want to find all the JavaScript and CSS files in a directory called App_Plugins, the command would be:

Get-ChildItem .\App_Plugins\ -Include *.js,*.css -Recurse

Searching keywords in text files

I usually have to review log files to figure out what happened with an application and you know that those logs can get really big. Let's say that I have to search the text "Order id: 345912" in a bunch of log files, the command would be:

Select-String -Path *.txt -Pattern "Order id: 345912"

Reading a log file information in real time

Reading a website's log file in real time can be very useful when debugging a problem, that can be done with the following command:

Get-Content log.txt -Tail 10 -Wait

Getting a domain's DNS information

What is the TTL of a domain? Where does it point? Is the CNAME record configured correctly? All of those are common questions when configuring domains and all answers can be found by using the Resolve-DnsName command. For instance, let's list my domain's information:

Resolve-DnsName -Name camaya.co

Opening files

How do you open a .pdf or a .docx file without using the Windows explorer? you can use the & symbol for that, for instance:

& requirements.docx

Other common tasks

About commands names

I wrote all the examples above using the full name of the commands (or cmdlets in Powershell jargon), but I actually use aliases when working with them. For instance, ls instead of Get-ChildItem or cat instead of Get-Content.

Aliases save me a few keystrokes and they're easier to remember. If you want to know the available aliases of a cmdlet you can use the Get-Alias cmdlet, for example:

Get-Alias -Definition Get-ChildItem

You can even create your own aliases, but I'll leave you figured out how to do that ;).

Summary

As you can see, there are a lot of things you can do without leaving the comfort of your keyboard by using Powershell and there are a lot more so if you are a terminal fan like I am, you should definitely learn to use it, the Get-Help cmdlet is a good way to start :).

If you have any comment, suggestion or question you can reach me on Twitter (my DMs are open) @_camaya or you can send me an email to <cam at camaya.co>.

Cheers.