PowerShell: The object-oriented shell you didn't know you needed
chriswarrick.com
chriswarrick.com
Since pwsh 7.4 it can natively pipe the raw bytes between two external binaries and also when redirecting to a file. Sucks it took until then to get it but it's now there.
The end result was still a much better language than bash, of course, but that's a very low bar.
In my mind, it competes more with Python than Bourne shell: it's a scripting language that only tenuously gives you a DSL for interacting with processes and the streams between them.
Bash via WSL saves the day, in this regard (e.g.: $results = wsl ./myAwesomeBashScript.sh).
While PowerShell is great as object-oriented scripting, it still can't handle simple tasks that can be done in the likes of bash, zsh, etc.
Write-Host 'Foo$Bar'
Write-Host "Foo`$Bar"Granted - the Powershell way of doing some things is stupidly obtuse sometimes.
Perhaps, it's easy enough to do some .NET manipulation, just to get PowerShell to be able to handle the string for the next Invoke-RestMethod -- but its far easier to just curl those urls, instead.
[1] - https://learn.microsoft.com/en-us/rest/api/keyvault/keyvault...
The article covers all the main (minor) bugbears.
It expresses an arrogance and disregard for their users and makes a group of people just walk away from it. That’s totally unnecessary.
You can actually call .NET methods with completely nuts casing: "asdfg".tOupPer() works.
Also variable names with only different cases resolve the same, i.e. $var is the same variable as $Var.
String comparison with PowerShell operators is case-insensitive and it has special operators to match case sensitivity. RegEx operators behave the same way. Using the .NET methods however conserves case-sensitivity.
I enjoy using PowerShell a lot. In many of places for scripting processes (Windows server adminisitration) it has an easier entry point than Python. And handling complex objects is kind of trivial. The Windows PowerShell really shows its age by now though and I hope the new PowerShell will be included by default, too.
The Debian dash POSIX shell can compile under 100k on i386.
This is a space where Powershell can never be.
You’d only ever use it to work with Microsoft stuff though, and older Microsoft stuff at that.
The speed and economy of size are a side effect.
This isn't to say I am not critical of some aspects of PowerShell. I am simply saying that being critical about compiled software size is something that left the building a long time ago as a valid concern. I am very much into efficiency and performance in software. As much as I hate it, these are not metrics that make the difference between winning and losing for most software applications today.
Portable scripts.
I wonder how big PowerShell would be if it was reimplemented in WinRT instead of .NET
Backticks for line continuations was also a satanic decision, even knowing of the myriad alternatives to them in PoSH.
I remember being fascinated by its history because the main guy wrote it, Jeffrey Snover, I'm going to butcher this, but IIRC he was some sort of big posix/shell/bash/perl/something-very-shell-important and wanted to take the best of whatever systems he was familiar/contributed/wrote and improve them and make that powershell.
Anyway, yeah I absolutely despise using it. It's syntax is atrocious. It's key words aren't memorable in the least. I know some people absolutely love it, even people who aren't using it in Windows. But it's never clicked for me.
I found this thread interesting. I guess Snover left MS after 23 years there in 2022, and he was demoted at some point after creating powershell. I don't care enough about it to dig into why but if anyone is interested- https://news.ycombinator.com/item?id=31302174
I did windows engineering at the biggest webhosts you know, RAX, HG, etc. Being a Windows Eng in webhosting is incredibly uncommon. Prior to that and post I'm back as a bsd/linux engineer (distsys/sre now) and the Windows Server ecosystem is absolutely abysmal if you aren't in it for Exchange, MSSQL, or whatever random Windows-Only app your company uses. I supported that trash from server 2000 - 2012 r2.
Never again.
(... and set -euo pipefail was default). Bash scripts can be tiny for what they do, and the ssh integration is unmatched.
xonsh is nice but it's been unstable for me lately.
nushell is nice but not sure if it's worth switching the default for.
[1] https://github.com/dahlbyk/posh-git [2] https://git-scm.com/book/ms/v2/Appendix-A%3A-Git-in-Other-En... [3] https://learn.microsoft.com/en-us/powershell/module/microsof...
But sometimes the "everything is an object" is useful in ways that would be really hard to do on unix like systems.
Actually on Linux I tend to use Ruby for any non-trivial scripting, and it feels much closer to that. So it's probably no coincidence that I've got on with it.
Also, I don't know what it is with shell programmers (and I've worked for one) but bash syntax is similarly insane. I am so glad I've never had to write an init.d service because 20 years into my linux career I still can't read that shit. Like I can launch and fork a process in like 10 lines of C, but it's five pages of gobbledygook shell script?
Generative AI can do a lot of fun things, but we're talking about a but it does not belong anywhere near an interactive shell.
It writes impeccable code - but doesn't always get the question right. I've found most of the time, even when it gets the question wrong, it turns you onto the right path forward. Definitely has saved me a lot of time researching things.
Yeah. At a certain point it is almost impossible to internalize that complexity.
As someone suggested...in the era of AI, this might not matter at all.
I write C# daily but I'm not fine with powershell syntax. It seems harder than it should be.
At least it’s terse, which is good when you’re typing commands on the fly on the command line. PowerShell on the other hand is so unwieldy on the command line. Even the pervasive use of case slows down typing.
(Get-ChildItem -Filter '*.txt' | ForEach-Object { $_.Length } | Measure-Object -Sum).Sum
You can do ls *.txt | measure -Sum Length | % SumBatch is not nearly as flexible as PoSH although the interpreter still boots faster so I use cmd.exe all the time.
You're not wrong. I think it's that way because of trying to increase readability. Best practice for Powershell cmdlet development is verb-noun.
Of course they throw that out the window with aliases.
Examples:
wget and curl both are aliases for invoke-webrequest
iex is an alias for "invoke-expression" - which lets you execute a string as powershell code in your same session (note: I originally wrote scriptblock, which was incorrect - otherwise something I wrote this week wouldn't work at all, due to change of scope). This ability gets me out of a lot of weird jams.
The other thing too which makes it so weird is that it is built on top of .Net. The overarching principle of Powershell is that it's your Windows swiss army knife. You can access .Net classes by loading the assemblies. I have on occasion built a small WinForms app in powershell. You can create and invoke COM objects (new-object cmdlet).
> But as a polyglot I am so sick of having to learn yet another syntax.
Personally, I try to avoid "language specific sugar" when I am writing sample scripts for people (I am in a customer facing role). But goddamn I love the shorthand way you can do a for loop in Powershell using the range operator:
1..100 | % {Write-Host $_}
> cmd.exe does everything I need to and batch is actually kind of flexible.
OK, so you haven't really gotten to know Powershell very well if you're saying things like this. lol
Scripting in batch files is like living in the dark ages. They are "kind of flexible" whereas Powershell is "very flexible, most of the time".
And that "most of the time" caveat is no longer there if you are on the latest powershell (which doesn't work with the ISE as ISE is 5.x only, and the way of the future is the universal PWSH). My biggest gripe was fixed I think in Powershell 7, which was manipulating files with grave marks in the name.
I build a lot of stuff in Powershell, as you can probably tell. Given all the interop stuff it has, once you know how to use it all, the point where it makes sense to build it in C# is farther down the line than you might think.