PowerShell-ing

I've always despised Windows scripting capabilities due to being really crappy: Either the old MS-DOS days .BAT files, which were ok in the early 90s but became obsolete quite fast, or the dreaded .VBS, taking the worst of both worlds (Visual Basic and OS scripting) and giving birth to ugly and awful scripts.

I've also admired since long how Linux had really powerful scripting capabilities, and questioned why Microsoft didn't copied them. Probably their policy of "eat your own dogfood" even if is VB scripting.

Now that I work many hours daily with Linux, I'm starting to understand why people sometimes builds scripts instead of "real tools" with compiled binaries: You shoudn't need all the compiler and extra code steps for simple tasks (or to change them in the future).

Powershell has been for quite some years around, but apart from reading of his capabilities I didn't had any urge to use it. While it really takes a bit to "boot up" (more than a normal command prompt, that is), and the errors are sometimes too verbose, I've started to check some quick tutorials and specific how-tos in order to stop building C# binaries for trivial tasks.

Last year I built a small images thumbnail generator, that grabbed all JPGs in a folder and stored 150x150 thumbnails in a subfolder, creating it if didn't existed, and saving a "log" file.

This week, I used that code as the base for building an image watermarking script; put a "watermark.png" image in the folder where you have the JPGs you want to watermark, and it will add it to the lower-right corner of each image, saving them in a subfolder.

You can check both scripts at my small PowerShell github repo.

Other useful commands I've learned/gathered

Mass file renaming (sample replaces whitespaces by underscores):

Dir | Rename-Item –NewName { $_.name –replace " ","_" }

Tail (folder level, listens for changes on multiple files):

Get-Content \path\to\files\*.log -wait

NSLookup:

Resolve-DnsName kartones.net

TraceRoute:

Test-NetConnection kartones.net –TraceRoute

Netstat:

Get-NetTCPConnection | ? State -eq Established | FT -Autosize

IPConfig:

Get-NetIPConfiguration

Ping:

Test-NetConnection -ComputerName kartones.net -InformationLevel Detailed

While any Linux user might joke about how fat and slower is in contrast with .sh scripts, truth is that having access to the full .NET Framework allows you to do really impressive things.

I will probably migrate more C# snippets that I have to PowerShell next time I use them. This way I stop needing Visual Studio installed for quick changes, which despite of having in a VM, I'm becoming lazy to boot a virtual OS for only that reason.

Update: Added other commands I tend to use when operating with Windows machines (not so often now).

Tags: Development

PowerShell-ing article, written by Kartones. Published on