Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Muhammad Azeez
Muhammad Azeez

Posted on • Edited on • Originally published atmazeez.dev

     

Why I love Powershell as a scripting language

Every once in a while, I have to write a script to automate a task. Maybe it's part of a CI/CD pipeline, or it's part of my dev workflow. My favorite language for writing scripts is PowerShell. Here is why:

1. Powershell has an object pipeline

When you pass data (or pipe them) from one command to the next, the data is treated as an object, not as a string. Which means the data can have different properties and it retains these properties.

This is very powerful and unlocks all kinds of composability scenarios. Commands don't have to be single purpose, they don't have to provide different switches to get different outputs. They give you everything and you can use only the properties you need.

2. You have the full power of .NET

Powershell is a first class programming language on .NET. You can do almost everything with Powershell. that you can do with C# or F#.You can even create GUIs if you want to. This means that you have access to the coherent and well designed Base Class Library of .NET as well as the entire gallery ofnuget packages. It also hasa lot of modules of it's own.

3. You can use Select/Where/GroupBy

Because Powershell is built on top of .NET, it has access to .NET's version of map/filter/reduce. They make scripts much more pleasant to write and read. However the names are a bit different from Javascript or other languages:

JS NamePowershell
FilterWhere
MapSelect
ReduceMeasure / ForEach
ReduceGroupBy

4. It's now cross-platform and open-source

Powershell Core is a cross-platform and open-source version of Powershell built on top of .NET Core maintained by Microsoft.

5. Working with Data is easy

You can easily work withExcel,XML andJSON data which makes working with APIs much easier.

Some cool examples

Here are some cool examples that demonstrate the points above:

Get top 3 largest files in a folder

Get-Childitem'C:\Windows\System32'|WhereLength-gt(10MB)|# Only files that are greater than (-gt) 10 MB (MB is a constant in PS!)Sort-Descending-PropertyLength|# Sort files by their length ascendingSelect-First3Name,Length# Only select name and length properties (projection)
Enter fullscreen modeExit fullscreen mode

Output

Name                    Length----                    ------MRT.exe              131005360nvcompiler.dll        40444864WindowsCodecsRaw.dll  32612880
Enter fullscreen modeExit fullscreen mode

Calling a REST API

In this example we call a JSON REST API and print a property

$uri='https://cat-fact.herokuapp.com/facts/random'# random cat fact API$fact=Invoke-RestMethod-Uri$uriWrite-Host$fact.text
Enter fullscreen modeExit fullscreen mode

Output:

Cats make about 100 different sounds. Dogs make only about 10.
Enter fullscreen modeExit fullscreen mode

Export all process information as an excel sheet

Get-Process|Select-ObjectCompany,Name,Handles|Export-Excel
Enter fullscreen modeExit fullscreen mode

Result:

CompanyNameHandles
Microsoft CorporationCalculator537
Google LLCchrome449
.........

Note: You'll have to installImportExcel module for this example to work.

Importing data from excel

Consider this excel sheet:

CityPopulation
Erbil1500000
Sulaymani739182
Duhok1293000
Kirkuk1598000
Halabja245700
# Get the average poluation of cities in Kurdistan:Import-Excel'F:\cities.xlsx'|Measure-Average-Propertypopulation|Select-PropertyAverage
Enter fullscreen modeExit fullscreen mode

Note: You'll have to installImportExcel module for this example to work.

Note 2: Data is from Wikipedia.

Ouput:

1075176.4
Enter fullscreen modeExit fullscreen mode

While my preferred scripting language is Powershell, I strongly believe everyone should use whatever tools/languages they are productive in. There is no best language. Everyone has different preferences and that's okay.

If you're using Powershell on Windows, I suggest you readthis post to make your experience even better.

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
artydev profile image
artydev
  • Joined

Hy
Thanks for your post
I have Always found Powershell a little cryptic.
If i had to administrate Windows servers, I would use cssript.
Regards

CollapseExpand
 
mhmd_azeez profile image
Muhammad Azeez
I am a Software engineer from Kurdistan, Iraq. I am a MCP in C# and a MCSA in UWP. And I also ♥ Open Source!
  • Joined
• Edited on• Edited

That's okay. Everyone has their own favorite scripting language. But I encourage you to give Powershell a chance. It's a beautiful language once you learn it :)

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a Software engineer from Kurdistan, Iraq. I am a MCP in C# and a MCSA in UWP. And I also ♥ Open Source!
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp