147 points · by japaget · 10y ago · 161 comments
Bash and Unix commands are only "intuitive" after years of practice; Powershell is not different. And it doesn't have any obligation of being equal to bash.
I see it this way, the great thing about Powershell commands is that I get back an object. The great thing about Bash commands is that I get back text. The object is great because I don't have to parse it. The text is great because I can parse it.
The downside of Powershell is that if I don't have technical expertise in regard to the object a command returns, I have to go read deep into a manual to figure how I can do something simple. The downside of Bash is that if I have deep technical expertise about the text that is returned, I still have to parse it as text to do something simple.
The high level abstractions of Powershell are better suited for dealing with known processes and contexts where I care about the semantics of the returned value. The low level abstraction of text is better for dealing with unknown processes and contexts where I don't really care about the semantics of the value.
Is it possible for Powershell to output the object as JSON so that you can see what you can read from the object?
At that point, I either know the name of the method I'm looking for or I don't. In the first case the conversion to JSON doesn't do me any good because I can just call the method directly using `object.method(value)`. In the second case, it doesn't do any good because I don't know the method I want.
Reflection looks more like this: http://stackoverflow.com/questions/9952391/use-get-member-fo...
dir | ConvertTo-Html
dir | ConvertTo-Xml
dir | ConvertTo-Csv
Things I don't like, or is a pain point for me, are those same objects. As far as I can tell, there isn't a better way to figure out what you actually are getting back from a cmdlet besides feeding output through Get-Member.
For example, what comes back from dir/ls/Get-ChildItem? Well that's a trick question because it depends on which "provider" you invoke. On a file system (dir somedirectory) you'll get back FileInfo or Directory info, for a registry hive (dir hklm:) you'll get a RegistryKey, for environment variables (dir env:) you get a DictionaryEntry, etc.
So while it is fantastic there is a general way to enumerate items, the docs have to be vague and list System.Object as what returns from Get-ChildItem and its various aliases, which isn't very useful for working with the results.
This makes forming non-trivial commands with pipelines fairly annoying because I have to feed each stage through Get-Member to even figure out what I have and what is now available; this strategy starts to break down if a pipeline stage feeds a loop, etc.
I'd love it if the ISE displayed what actual object type exists at every point of a pipeline, rather than the trial-and-error-by-Get-Member approach I've adopted. Or for there to be some better way to get the return type of a cmdlet.
PowerShell is a huge improvement over the old command prompt though.
If you look at the note properties that get added to the objects, they're consistent across providers:
PS C:\> (Get-Item c:\).PSPath
Microsoft.PowerShell.Core\FileSystem::C:\
PS C:\> (Get-Item env:username).PSPath
Microsoft.PowerShell.Core\Environment::username
PS C:\> (Get-Item HKLM:\software).PSPath
Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software
There's also some magic that you can do with tab completion to see the properties. For example, if you type...
(Get-ChildItem c:\ | sort).
...and hit tab, you can tab through the list of properties of the objects returned by Get-ChildItem when it's used on the FileSystem provider. (Don't ask me how the hell that works, though...)
The excessive verbosity and object safety is great for admin scenarios where you don't suddenly destroy your corporate email transition because of a VP with an apostrophe in her name. But at that point perhaps you should just use C# instead?
But for everything else it just looks nuts to outsiders. That's why even the MS developer groups are releasing command line tools cross-platform that behave like Unix apps, not PS monstrosities.
https://azure.microsoft.com/en-us/documentation/articles/xpl...
Powershell should be a feature that draws people from AWS. Instead it is seen as a drawback, even inside MS.
Learn some alias's if you don't like the Get-X style of functions.
gal * | more
Will list tons of alias's for you.
dir -recurse | where {-Not $_.PsIscontainer -AND $_.name -match "_"} | foreach { $New=$_.name.Replace("_"," ") Rename-Item -path $_.Fullname -newname $New -passthru }
EDIT: I purposely posted the first Google result. You obviously Googled as well as your code got it backward. But really, why isn't it this? It's worked since the 1980s. You'll dig it since it uses extra apostrophes.
rename '_' ' ' *I once wrote a PS script to set some power management options on a corporate laptop. The script was 11 lines long (vs 3 lines in Python), it was harder to read and understand. Even with ISE available it was much harder to write.
I followed along with some exchange server tutorial where the prescribed easy way to achieve the task was a completely unintuitive PS script. I kept thinking about how much easier that particular config change was to make on a Linux host running postfix.
I can't see what benefit PS has over cygwin bash but I'd love to hear ideas because PS comes installed out of the box where I have to install cygwin myself.
Now of course the implementation of typed pipelines in PowerShell is clunky and forced, because typed I/O primitives do not (yet) actually exist in Windows, so all the piping is done within one process with streams of object pointers, else it doesn't work. There are other problems, too: I recall one advocate linking me to the PowerShit solution to some admin task like changing the ACLs of a directory tree recursively. It involved including a C# program as a herestring, passing it to the C# compiler, and loading the resulting bytecode. The advocate said "Isn't this beautiful and elegant?"
But in principle the model embraced by PowerShell is strictly more robust than bash's fragile "dumb byte pipes" model.
"Get a byte, get a byte, get a byte, byte, byte!" --NT architect Dave Cutler, on Unix I/O
Luckily modern UNIX-based systems are rarely used as multi-user terminals these days or we would see many more privilege escalations via exploiting bugs in scripts which iterate over user owned files. Even in web scenarios, most web frameworks carefully manage what is allowed in a filename or set the filename themselves.
Ultimately because on UNIX there is no type, you have to make assumptions. These assumptions are sometimes dangerously wrong. You can definitely write scripts and execute commands in a more secure way, but you can go google Linux tutorials right now and find tons of examples where newbies are taught to do it wrong out of the gate.
Data types make this "secure by default." String for everything requires the script-writer or command line-writer to be perfect every time they hit return.
[0] http://www.defensecode.com/public/DefenseCode_Unix_WildCards... [1] https://www.helpnetsecurity.com/2014/06/27/exploiting-wildca...
If you want to write a good program, use a programming language that cares about good programming. Bash et al. don't care about how good the program is, they care about getting something done quick.
Note that this might be true for one script, and the opposite might be true for another script. Something is saner in bash, something is better in PS. They have fundamental differences (text- vs. object-based), and neither of them is better than the other in all scenarios. (At least that's my experience.)
With Bash you get a wealth of tools because everything can handle text.
If there's anything wrong with the article (for one thing, it's 4 yo), please point it out. I just brought this up because the IronPython part of .NET is usually forgotten, though still there and has some interesting uses.
[0] http://www.pythondiary.com/blog/Jun.26,2012/ironpython-and-m...
... and all those tools work seamlessly in Powershell, too. .NET objects are passed around within Powershell, but traditional dumb text is used for out-of-proc stuff. Where is the "fatal flaw?"
I don't understand where the perceived downside is - you wish Powershell was implemented in Python, not .NET?
I think technically it should be no problem to write some package for most programming languages for interopting with .net objects. In this sense about everything could also handle .net objects.
And no autocomplete.
| :PowerShell | Bash: |
|Tab completion | built-in, OOTB|
|Get-help | `help`|
|get-command | `apropos -s <section> .`|
|show-command | `<command> --help` or `man <command>`|
|get-member | Inapplicable; text has a structure. Use `head -1` to find out, given structure is printed on first line |
|get-alias | `alias`|
Do I remember the name of the signing cmdlet(s)? Nope! Is this really better than just using alias, or changing the PATH? Probably not.
Another reason is vendor support. For example, VMware publishes a set of PS cmdlets as 'PowerCLI' for doing stuff in vSphere, but if you want to do something outside PS you're going to have to script/code against an SDK. I suspect there are other vendors doing the same thing. That may be a PS benefit. I usually end up writing a script instead although I found e.g. bulk movement of VMs from one set of datastores to another easier in PS.
The final thing I can think of is that there are a few situations where maybe you want things to stay in-process, and you get that w/ PS whereas you wouldn't with bash. An example I have is a script that facilitates remote deployment of an MSI with impersonation of a user on the remote machine. This is pretty straightforward with a WMIObject in PS, but not in cygwin bash since Windows doesn't really have sudo or su -c (runas only works interactively) and wmic doesn't offer any impersonation capability.
None of the above are really compelling for me, I end up spending 99+% of my time in bash/tmux/mintty.
For Exchange server administration, it is very useful.
Enter-PSSession give you a remote shell. There was news a while back that Microsoft was going to port OpenSSH to Windows, so things will improve.
I still prefer a Unix environment; tools like find are just a must-have. But compared to cmd.exe, which is the baseline many Windows people look at, it is a huge improvement.
For serious scripting, I prefer Perl, though.
gal * | more
A whole list of short alias's
Funny, from a guy coming from Windows to Unix I think the exact opposite. Each command comes with a bazillion options and the defaults are simply atrocious (to read the man, oy). Yes I understand that you can tailor each command in some startup script but why? It almost feels like every command needs to be created to solve 5 million things when 99% of the time you use it for 1 or 2 things. A few of my favorites:
Scripting:
A = 123 vs A= 123 vs A=123 should be the same!
Please use trim() in your program when parsing tokens!
Keyboard:
Please, for the love of god, make the keyboard behave like a keyboard. I don't want to be logged into some terminal and have the Backspace not behave like a backspace (^H??). Yes, I'm sure I can make it work via some, again, configuration magic or enter emacs mode, but, again, why?
cd.. should just work.
</rant>
Having been on unix for the last 7mo after years of windows, I do like some things but let's not be too quick to call the kettle black.
You don't have to do any magic for that normally. Even if using just xterm with bash, backspace should behave like backspace almost everywhere. You had some weird configuration there – possible you used the terminal of a greybeard?
Each has their strengths and pain points. There's a reason normal people use Windows and Power Users gravitate to Linux.
So, Windows+Python it is. If you really miss the command line utils, theres the GNU Core Utils package to solve your life (though pipes still behave slightly different and that may show in a contrived command).
If so, use nc or ncat, telnet expects a terminal at the other end, something no system exports anymore. It's only there for interaction with ancient configurations.
Who's calling the kettle black? You seem to be painting this as a Windows vs. Unix thing, but the original article never mentioned Unix.
> cd..
So your rant is about the syntax, of a language established for decades. You have options though: modify bash, or raise awareness enough that a simple `alias cd..='cd ..'` is included in the default release.
It thought it would come naturally over time, but it never does. It seems needlessly cryptic and verbose in so many ways. I've started using LinqPad for stuff I would normally do in cmd now.
Some of it stems from the fact that i just don't use it enough, but most of it from the fact that this shit just isn't intuitive. why is -b the flag for key length in ssh-keygen? Why is -v the flag for "don't include" in grep? I have those memorized now, but there is no way they are "intuitive".
Because the length is given in "bits". I get your point, but that's a terrible example! :-)
As user yread said, it's for inVert (the sense of the pattern match, from "matches" to "does not match"). You're right, it is non-intuitive.
One of the issues is that when you use single letter options, a particular letter could potentially stand more than one expanded word, e.g. in the case of v, it is more commonly used for Verbose or Version. Even there, it varies. Some commands use -v for version, others use -V (e.g. python does), and some use --version.
GNU's long option format is a partial solution to that, but involves more typing. E.g. --verbose or --version or --invert. Also the command lines get longer then. No perfect solution as of now.
You're right, over the years, with more and more people creating commands, and with no well-defined standard in the earlier years at least, the whole scene of command line tool names and options has become something of a mess. Later one some sort of standards and conventions have been created or evolved, informally, but not many command creators necessarily follow them.
http://pubs.opengroup.org/onlinepubs/009696899/basedefs/xbd_...
Also, they are pretty detailed, so it is somewhat time-consuming and complex to implement those guidelines in full.
I have exactly the same problem.
I use Linux since about 8 years but somehow I just don't use the commandline very often so I forget stuff all the time. Because of this I have half a dozen post-its glued to my monitor for all the grep/git/whatever tricks I just can't keep in my head. And for other stuff I use about once a year I have a long linux_tricks.txt on my desktop where I write down all the tricky and obscure command line stuff.
We use powershell a lot at work for developer setup stuff and other common tasks we do on a daily basis and at this point I've considered just writing my own language to do it or find something else.
I wanted to learn Ruby a while back but didn't have a web project to work on so I started writing a load of scripts in Ruby instead. It worked out fine.
Don't know much Ruby or Python to be honest so I'm not sure how could I apply them but I'll dig around sometime soon.
That isn't much comforting. And frankly, MS always pushed Power Shell as a better way to do things than the ancient options for command line, thus color me disappointed that it couldn't be better than the "default choice" Bash.
Posh also suffers in its somewhat easy to invoke any .NET library, so people start comparing it to a more tooled programming language or something with more sane libraries, and the "ISE" is nowhere close to an IDE (why isnt powershell a first class citizen in visual studio! yes I know there are tools, they are not great.)
Nonetheless, after a few years of working in sql server I have adopted more and more posh in my life because its simply the fastest way to get things done if you are doing windows server management.
I usually find myself on one of these two places if I am looking for something:
Mike Fal (SQL Server focus, he is great) http://www.mikefal.net/
Hey Scripting Guy! https://blogs.technet.microsoft.com/heyscriptingguy/
edit: forgot to mention that MSFT helped to cause problems by massive changing how the language did things over time(usually for the better), so ps1 vs ps4 is crazy different, another reason to be annoyed because which version comes with which product is a fun skew to deal with (thanks sqlps having ps2.0!)
Sure, I'm not thrilled with having to learn another language to get anything done. But it's so much better than what came before that I don't care. Besides, Bash and friends certainly have their quirks as well.
In the past, say, six months, I cannot remember having any trouble with it.
"Clink combines the native Windows shell cmd.exe with the powerful command line editing features of the GNU Readline library, which provides rich completion, history, and line-editing capabilities. Readline is best known for its use in the well-known Unix shell Bash, the standard shell for Mac OS X and many Linux distributions."
"ConEmu-Maximus5 is a Windows console emulator with tabs, which presents multiple consoles and simple GUI applications as one customizable GUI window with various features."
What issues did you face?
This on windows 10 with it's nice new command window is pretty nice, but I still do anything serious on my macbook.
The Author does say "subjectively" but in my experience, both personally and with others, the reverse is true. Powershell can seem to be really cryptic.
That said the return semantics stink.
I also use MobaXTerm as my Putty replacement. It also comes with a Bash terminal that works fine. Very handy!
(Two reasons why I love to have learnt Bash)
Granted, bash may not be the most intuitive shell ever, but it just works about everywhere and this is a huge advantage when switching OS regularly.
It's the other way around: One of the reasons I don't use Windows is because I have to use the command line.
There's also other handy improvements, like typing "cmd" into the file explorer location bar to open a command window to the current location you are viewing.
Windows 10 fixed that with the improvements you mentioned. It's still no "Terminal", but it's now about 80% as good, instead of 1%.
This viewpoint is just plain wrong. Since GUIs typically don't have a way of doing sequences of commands other than clicking and waiting, then clicking again, or of automatically making decisions, the biggest reason to use a CLI would be to express things that you can't with a GUI - like anything that requires a flow-of-control with if-then-else.
After all, that's one of the reasons that rebuses are fun - they're indeterminate and it's fun to de-ambiguate them. It's less fun to have to do all de-ambiguation yourself, when a simple pipeline or while-loop would do the trick.
Maybe there are better programming patterns for me to use here. Coming from traditional imperative languages, I was confused though, especially when I got to nests of functions, return values, and debug outputs. I mean, I later understood why things worked like they did -- I just had a hard time wrapping my code around the philosophy. Which is unusual for me to have with a language from Microsoft in their post-.NET era.
I know what I'm thinking. One of the reasons I don't use Windows is that its command line is a second or third class citizen.
What should take 2 seconds to type takes 20 seconds to find in godawful menus, that get harder and harder to find with each new version of Windows.
The power of the unix command line is mostly from the utility commands themselves, not from the shell language/ecosystem. Nobody seems to be super enthusiastic about bash as a language (see all of the "at this point I just go to perl/python/etc" comments), but the commands themselves are super powerful and flexible. If you put in the time to really learn the commands and their flags, you can do quite a lot. Watching experienced unix users at the prompt, I routinely think "oh nice, that's a great command to have", but rarely "oh that's a handy language feature" or "wow Bash made that really easy".
With Powershell, it's kind of the opposite IMO. The language and ecosystem are mostly well-thought-out, consistent, and powerful. Everything-is-typed, pervasive and awesome tab-completion, mature pipeline system, decoupling of data and how it's displayed, access to the .NET standard libs, ease of extensibility and scripting (oh god it's so nice never neededing to parse arguments!), reflective help system and discoverability features, easily hostable in other apps... That all comes from the ecosystem. Where Powershell often falls flat is the limited set of built-in commands, or the lack of sensible flags/options for those commands which do exist. You're often falling back to legacy cmd.exe-era executable utils, or invoking some .NET API, both of which result in more verbose, awkward code. e.g. reading/writing the registry is SUCH A PAIN with powershell builtins (why?!?!?), so reg.exe it is. Much of that comes from Powershell's relative youth (the set of builtins and their flags is still evolving), and the relative script-unfriendliness of Windows as an OS compared to unix.
So for a particular action, in unix you have have a utility (or even N utilities) with 15 arguments to handle 95% of possible cases, in Powershell you'll get a cmdlet with maybe 5 or 6 arguments that handles 75% of cases, but the output is an object so the remaining 25% can be done by calling some method or grabbing a field. Both approaches have their strengths and drawbacks. The Powershell approach tends to scale better, I'd say - you can create fairly large scripts/packages in pure Powershell without needing to bail out to Python or C#.
except that there's tons of drawbacks to counteract said benefits. Privacy notwithstanding, power-use has been fractured and the flow of configuration ruined in 10
This is 2016, CP932(Shift JIS with IBM extension) shall be abandoned.
But that was 10 years ago, it disappeared at some point, being integrated somewhere in either the server version or the professional edition (named something like Unix Service for Windows) I lost track.
Considering all the work that MS is putting in interoperability those days, I'm wondering if it will not make a come back at some point.
Still CMD is worse, and Powershell is different, but maybe not any better...