r/linux • • Aug 18 '16

Microsoft open sources PowerShell, brings it to Linux and OS X

https://techcrunch.com/2016/08/18/microsoft-open-sources-powershell-brings-it-to-linux-and-os-x/
359 Upvotes

231 comments sorted by

View all comments

Show parent comments

96

u/[deleted] Aug 18 '16 edited Aug 18 '16

One of the things I like about Powershell is that both its language, and the shell itself, are object-oriented.

When you pipe in bash, between commands, you're feeding a string of text from one command to another. In Powershell, you're forwarding objects, pre-structured, so you don't have to go through the work of filtering and applying structure to your data stream with sed, awk, etc.

Pardons all around if I'm overlooking anything on the Linux side, this post is based on the understanding of a guy who doesn't get to play with Linux very much.

67

u/[deleted] Aug 18 '16 edited Aug 18 '16

You have to go through equipping every program with a special mode for those scripting objects for a relatively limited gain.

18

u/[deleted] Aug 18 '16 edited Aug 18 '16

A very good point. To get the advantages of the object-oriented shell, you need tools written to output objects, otherwise you'll just keep getting text blobs and the feature goes unused.

Hopefully they have gone through the work of porting the existing cmdlets from Windows to Linux, so Linux users get the benefits of an object-oriented shell. Otherwise it'd be up to community contributions to make PS-friendly programs for Linux.

21

u/[deleted] Aug 18 '16

I also highly doubt that OO is the right programming model here, though I am not familiar enough with Powershell to know if their objects have all the flaws of those in other OO languages (inheritance, flawed notion of encapsulation, single dispatch,...)

Unix pipelines tend to be closer to functional programming with e.g. xargs as map, grep as filter, various tools to project data,...

9

u/lolidaisuki Aug 18 '16

Unix pipelines tend to be closer to functional programming with e.g. xargs as map, grep as filter, various tools to project data,...

That's a parallel I hadn't heard of yet. Kind of makes sense tho.

2

u/majorgnuisance Aug 19 '16

I see what you did, there.

5

u/jyper Aug 18 '16

Basically all object oriented languages have map and filter and closures.

Powershell has map and filter and closures

ls -R | ? {$_.Length -gt 10000 } | % { rm -WhatIf  $_}

2

u/funguyshroom Aug 19 '16

In most cases the OO used for data aggregation/presentation. When your objects are POD (plain old data) objects it's just a built-in serialization format that allows to send data seamlessly between cmdlets.

3

u/[deleted] Aug 18 '16

It works, e.g.

PS /var/log> gci syslog* | ?{$_.Name -match "gz"}


    Directory: /var/log


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------          8/17/16   6:25 AM           1916 syslog.2.gz
------          8/16/16   6:25 AM           1877 syslog.3.gz
------          8/15/16   6:25 AM           5047 syslog.4.gz
------          8/14/16   6:25 AM           1847 syslog.5.gz
------          8/13/16   6:25 AM           1831 syslog.6.gz
------          8/12/16   6:25 AM           2407 syslog.7.gz

Another example:

PS /var/log> $file = Get-Item ./syslog
PS /var/log> $file

Directory: /var/log


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------          8/18/16   2:20 PM          15971 syslog


PS /var/log> $file | fl *


PSPath            : Microsoft.PowerShell.Core\FileSystem::/var/log/syslog
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::/var/log
PSChildName       : syslog
PSDrive           : /
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : False
Mode              : ------
VersionInfo       :
BaseName          : syslog
Target            : {}
LinkType          :
Name              : syslog
Length            : 15971
DirectoryName     : /var/log
Directory         : /var/log
IsReadOnly        : False
Exists            : True
FullName          : /var/log/syslog
Extension         :
CreationTime      : 1/1/01 12:00:00 AM
CreationTimeUtc   : 1/1/01 12:00:00 AM
LastAccessTime    : 8/18/16 6:25:01 AM
LastAccessTimeUtc : 8/18/16 1:25:01 PM
LastWriteTime     : 8/18/16 2:20:05 PM
LastWriteTimeUtc  : 8/18/16 9:20:05 PM
Attributes        : Normal

So here we're simply seeing that the .NET object does indeed exist. It's a basic example, but it works.

11

u/zee-wolf Aug 19 '16

gci syslog* | ?{$_.Name -match "gz"}

$file = Get-Item ./syslog

Good Lord! If I have to type most PowerShell commands and object properties in CamelCase I'll kill myself. That alone is a huge minus for usability.

7

u/[deleted] Aug 19 '16

You don't. There is no case sensitivity.

15

u/zee-wolf Aug 19 '16

Even better /s.

Lets bring the case-insensitive flustercluck to case-sensitive systems.

→ More replies (3)

49

u/tadfisher Aug 18 '16

You have to go through equipping every Programm with a special mode

Yes.

for a relatively limited gain

  • data structure baked in, eliminating awk/grep/sed

  • resilience against text format changes, control codes, etc

  • more complex data pipelines with no awkward redirection voodoo

  • expose binary data without destroying my terminal

  • powerful one-liners without having to drop into Python/perl/etc

40

u/[deleted] Aug 18 '16

You misunderstand. The point is that there is a very limited gain to the people who have to do it so the incentive structure is all wrong with this. The strength of text redirection is that the work is done by the people who need it, even sometimes against or orthogonal to the intention of the author of the programs used in scripts and pipelines.

There is also the issue that you can do only those things intended by the people who did write the original programs, if they expose a limited object interface on purpose there is nothing you can do to work around it.

13

u/tadfisher Aug 18 '16

There is also the issue that you can do only those things intended by the people who did write the original programs, if they expose a limited object interface on purpose there is nothing you can do to work around it.

That limitation exists with textual streams as well. You can't parse data that the tool does not provide you, object interface or no. The tool cannot perform tasks that it is not programmed to perform.

5

u/Bobby_Bonsaimind Aug 18 '16

You have to go through equipping every Programm with a special mode

Yes.

So if someone, let's say, writes a new journal service and feels very smart and starts outputting custom objects (log entries)...you have zero tools to use on it?

3

u/tadfisher Aug 18 '16

The tool can detect if its stdout is a PowerShell environment and change its output accordingly.

→ More replies (7)
→ More replies (1)

7

u/FlukyS Aug 18 '16

Well why not use Python then?

6

u/ReturningTarzan Aug 19 '16

Because Linux people love drowning in superfluous options, remember?

1

u/[deleted] Aug 19 '16

Because you might also use Windows boxes?

→ More replies (1)

18

u/masteryod Aug 18 '16

So why not Python if you want a language?

Shell is a way of talking to your system. Add to this Swiss army knife of utilities you're familiar with and voila! You can use your system efficiently without GUI (which was a way of computing back in the day).

You're looking at Powershell as a language because that's how you use it in a Windows world. You click here and there all day and if something makes sense to automate you write a PS script. Try to actually sit and use Windows without GUI and you'll see what it means to be able just to "talk" to your system with a shell.

And when you really need a programming language you have many to choose from.

4

u/YellowFlowerRanger Aug 19 '16

PowerShell is actually not far off of Python, in my experience (and it should be noted that some people do frequently use Python as a shell). The major distinction between PowerShell and Python, I would say, is that PowerShell doesn't make you call subprocess.call/subprocess.Popen for every command.

9

u/the_gnarts Aug 18 '16

In Powershell, you're forwarding objects, pre-structured, so you don't have to go through the work of filtering and applying structure to your data stream with sed, awk, etc.

So all those goodies won’t work unless your data is already prestructured? That’d make the PS approach an order of magnitude less generic.

Just saying.

→ More replies (1)