Is PowerShell ready to replace my Cygwin shell on Windows? [closed]
Asked Answered
H

18

393

I'm debating whether I should learn PowerShell, or just stick with Cygwin/Perl scripts/Unix shell scripts, etc.

The benefit of PowerShell would be that the scripts could be more easily used by teammates that don't have Cygwin; however, I don't know if I'd really be writing that many general purpose scripts, or if people would even use them.

Unix scripting is so powerful, does PowerShell come close enough to warrant switching over?

Here are some of the specific things (or equivalents) I would be looking for in PowerShell:

  • grep
  • sort
  • uniq
  • Perl (how close does PowerShell come to Perl's capabilities?)
  • AWK
  • sed
  • file (the command that gives file information)
  • etc.
Hannahannah answered 21/2, 2009 at 19:42 Comment(10)
I wouldn't say that, I was interested in picking up Powershell, found this page, and now I know the general differences between PS and the shell scripting I'm used to.Theatheaceous
This post suddenly rose from ashes in a HN link submission. Great work. And bad for @Bobby for closing this as not constructive.Supervisory
If someone cannot ask how well one tool replicates the functions of another then S.O. cannot answer tool comparison questions. This is carefully worded to avoid controversy but was careless presumed "controversial" simply for looking like a Unix vs Windows question. And it got a factual, objective answer, with additional factual insight into how helpful Unix scripting might be on Windows.Victualer
Why is this closed, again? Someone please edit the title to say "PowerShell vs Unix Shells on the Windows Platform" to steer the trolls away from treating it as "Windows vs Unix," which it is not. This is a perfectly constructive question - voted to reopen.Piefer
I agree with the other commenters that believe this is a valid question with some helpful (and very popular) answers.Cycad
Personally, I think the title should change to something like: Is PowerShell ready to replace my cygwin shell on Windows? It seems to me that that's the real question here - it's not really a Unix vs Windows thing or a Unix Tools Vs PowerShell Thing. At the end of the day, the real question looks at whether PowerShell is ready as a replacement to existing Linux/Unix tools on Windows. Of course, Jeffrey's comment below points out that it's not an either or thing in any event!Archean
As written (particularly with the edited question), this is about a software tool commonly used by programmers, and it seems like a practical, answerable question.Herculean
The op is mixing shell with tools. PowerShell has its use. But GNU is a project to bring UNIX goodies freely to other OS, including Windows. Every thing in the list op gives have a gnu implementation in Windows. Accessible from GnuWin32 or individual sites. Windows BAT isn't useless, it has redirection and pipe and conditions.Vigilante
Cygwin is obsoleted by WSL (and MSYS before that). Perl has also always been available natively on Windows.Haehaecceity
Something like awk -F: {print $2} could be (-split ':')[1]. sed could be -replace. But PS is usually object properties not strings to be split.Idol
I
799

Tools are just tools.
They help or they don't.
You need help or you don't.

If you know Unix and those tools do what you need them to do on Windows - then you are a happy guy and there is no need to learn PowerShell (unless you want to explore).

My original intent was to include a set of Unix tools in Windows and be done with it (a number of us on the team have deep Unix backgrounds and a healthy dose of respect for that community.)

What I found was that this didn't really help much. The reason for that is that AWK/grep/sed don't work against COM, WMI, ADSI, the Registry, the certificate store, etc., etc.

In other words, UNIX is an entire ecosystem self-tuned around text files. As such, text processing tools are effectively management tools. Windows is a completely different ecosystem self-tuned around APIs and Objects. That's why we invented PowerShell.

What I think you'll find is that there will be lots of occasions when text-processing won't get you what you want on Windows. At that point, you'll want to pick up PowerShell. NOTE - it is not an all or nothing deal. Within PowerShell, you can call out to your Unix tools (and use their text process or PowerShell's text processing). Also you can call PowerShell from your Unix tools and get text.

Again - there is no religion here - our focus is on giving you the tools you need to succeed. That is why we are so passionate about feedback. Let us know where we are falling down on the job or where you don't have a tool you need and we'll put it on the list and get to it.

In all honesty, we are digging ourselves out of a 30-year-hole, so it is going to take a while. That said, if you pick up the beta of Windows Server 2008 /R2 and/or the betas of our server products, I think you'll be shocked at how quickly that hole is getting filled.

With regard to usage - we've had > 3.5 million downloads to date. That does not include the people using it in Windows Server 2008, because it is included as an optional component and does not need a download.

V2 will ship in all versions of Windows. It will be on-by-default for all editions except Server core where it is an optional component. Shortly after Windows 7/Windows Server 2008 R2 ships, we'll make V2 available on all platforms, Windows XP and above. In other words - your investment in learning will be applicable to a very large number of machines/environments.

One last comment. If/when you start to learn PowerShell, I think you'll be pretty happy. Much of the design is heavily influenced by our Unix backgrounds, so while we are quite different, you'll pick it up very quickly (after you get over cussing that it isn't Unix :-) ).

We know that people have a very limited budget for learning - that is why we are super hard-core about consistency. You are going to learn something, and then you'll use it over and over and over again.

Experiment! Enjoy! Engage!

Introrse answered 21/2, 2009 at 22:53 Comment(15)
Thanks for your answer. I think I'm going to go ahead and learn PowerShell. It looks powerful from what I've seen so far, and I'll be able to write more useful scripts with it at work.Hannahannah
You might want to start with the CTP3 of PowerShell V2. It is very stable and we've added some awesome stuff to it. As you encounter questions - ask them here - it will generate a good body of searchable knowledge.Introrse
I'm about to pick up PowerShell for the first time. I've actually read some pretty promising things from MSFT designers, developers, etc, which lead me not to get caught up in all the MSFT is evil mantra from around wherever. I enjoyed the smiley on "after you get over cussing that it isn't Unix." I really see a trend in Microsoft paying more attention to people's needs, across the board, and I'm glad to hear that this is one motivation of Powershell as well. I'll be happy to assess how this fits in my toolbox.Bullring
@Jeffrey: is there a chance for a better terminal for Windows? Powershell is a powerful scripting language, but the fact it runs in cmd.exe makes it much more convenient in the interactive modeRazorback
@sumek, @Jeffrey, exactly a better terminal for windows, that's all developers, admins want, why depart from the unix tools. You are telling us that you really couldn't make it work really well, fast and reliable using plain text file mentality of unix-like systems? Forgive my ignorance, i.e. you couldn't add windows terminal binaries to access objects and other configurations, and get rid of cmd.exe?Boley
A good answer, but I ran into a nasty little surprise with powershell. No multi-session history support! The only time it gets mentioned is a non-working hack from back in the development days.Winegrower
This "not constructive" question generated the best insight I've seen into the whole "on Unix everything is a file" mantra, and why Windows is different. Maybe StackOverflow would be better served closing not constructive discussions?Victualer
@Razorback - Give ConEmu a try; I've been using it for a few weeks and it's pretty sweet: hanselman.com/blog/…Saransk
@KeithHill, look at jeffre Snover history, he got 129 events(upvotes) on this question just on 13/07/2012, but just 20 where valued. What is going on ? I put a flag with this same comment but it was declined.Trilbee
@Victualer Although discussions bring insight in huge amounts (probably more than not-constructive, unanswerable questions), this is a Q&A site. Just because it's a terribly popular Q&A site doesn't mean it should experience scope-creep like that. Certainly using Q&A style, you most likely can't capture all human knowledge, but that doesn't change the fact that SO is Q&A.Luzluzader
Heads up: powershell doesn't like piping binary data. So beware when you're calling your trusty Unix tools, just don't do stuff like tar -c . | gzip > package.tar.gz directly in PowerShell, or you'll suffer. See brianreiter.org/2010/01/29/…Obnoxious
How should we provide this feedback?Gottwald
@Obnoxious - This is known behavior of powershell. It does encode output by default as utf-16. This is easily solved by adding -encoding ascii to the command line. This is really no different than having to specify binary mode for ftp.Obsessive
@ErikTheViking - Thanks. I remember asking on technet once about this problem and the Microsoft engineer told me to use cmd ... That was a couple of years ago though.Obnoxious
@ErikFunkenbusch Better yet, -encoding UTF8.Gottwald
R
125

grep

Select-String cmdlet and -match operator work with regexes. Also you can directly make use of .NET's regex support for more advanced functionality.

sort

Sort-Object is more powerful (than I remember *nix's sort). Allowing multi-level sorting on arbitrary expressions. Here PowerShell's maintenance of underlying type helps; e.g. a DateTime property will be sorted as a DateTime without having to ensure formatting into a sortable format.

uniq

Select-Object -Unique

Perl (how close does PowerShell come to Perl capabilities?)

In terms of Perl's breadth of domain specific support libraries: nowhere close (yet).

For general programming, PowerShell is certainly more cohesive and consistent, and easier to extend. The one gap for text munging is something equivalent to Perl's .. operator.

AWK

It has been long enough since using AWK (must be >18 years, since later I just used Perl), so can't really comment.

sed

[See above]

file (the command that gives file information)

PowerShell's strength here isn't so much of what it can do with filesystem objects (and it gets full information here, dir returns FileInfo or FolderInfo objects as appropriate) is that is the whole provider model.

You can treat the registry, certificate store, SQL Server, Internet Explorer's RSS cache, etc. as an object space navigable by the same cmdlets as the filesystem.


PowerShell is definitely the way forward on Windows. Microsoft has made it part of their requirements for future non-home products. Hence rich support in Exchange, support in SQL Server. This is only going to expand.

A recent example of this is the TFS PowerToys. Many TFS client operations are done without having to startup tf.exe each time (which requires a new TFS server connection, etc.) and is notably easier to then further process the data. As well as allowing wide access to the whole TFS client API to a greater detail than exposed in either Team Explorer of TF.exe.

Recidivism answered 21/2, 2009 at 21:37 Comment(5)
The thing is that the provider model is interesting only because the OS doesn't use text as its universal config medium, so you NEED these providers. With UNIX most languages have APIs to touch PAM, hosts, and packages, but ultimately text will always be there.Clemenciaclemency
Text is not always the best format for everything (start with databases and raster images). But I think we can agree to disagree rather than open format wars.Recidivism
Powershell can use any object in the .NET framework, doesn't this match Perl's domain capabilities? Plus you can write cmdlets in C# etc. if you want re-usabilityPadus
Point by point comparison. Nice one. This should be the accepted answer. PowerShell's strength lies in its .NET foundations and how easily you can extend the system by writing new Cmdlets or even calling class librariesIgnite
A typical use of sed (I think) would be: sed 's/pattern/replacement/' file, which is roughly gc file | %{$_ -replace 'pattern','replacement'}, and similarly for awk: awk 'BEGIN {} /pat1/ {action1} /pat2/ {action2} END {}' file is roughly {BEGIN {}; switch -r -c -file file { 'pat1' {action1} 'pat2' {action2}}; END{};}Semivowel
L
59

As someone whose career focused on Windows enterprise development from 1997 - 2010, the obvious answer would be PowerShell for all the good reasons given previously (e.g., it is part of Microsoft's enterprise strategy; it integrates well with Windows/COM/.NET; and using objects instead of files provides for a "richer" coding model). For that reason I'd been using and promoting PowerShell for the last two years or so, with the express belief I was following the "Word of Bill."

However, as a pragmatist I'm no longer sure PowerShell is such a great answer. While it's an excellent Windows tool and provides a much needed step towards filling the historic hole that is the Window command line, as we all watch Microsoft's grip on consumer computing slip it seems increasingly likely that Microsoft has a massive battle ahead to keep its OS as important to the enterprise of the future.

Indeed, given I find my work is increasingly in heterogeneous environments, I'm finding it much more useful to use Bash scripts at the moment, as they not only work on Linux, Solaris and Mac OS X, but they also work—with the help of Cygwin—on Windows.

So if you buy into the belief that the future of the OS is commoditized rather than a monopolized, then it seems to make sense to opt for an agile development tool strategy that keeps away from proprietary tools where feasible. If however you see your future being dominated by all-that-is-Redmond then go for PowerShell.

Loreenlorelei answered 7/1, 2011 at 17:12 Comment(4)
Does unix scripts work perfectly on Cygwin-Windows without errors?Phenanthrene
@Phenanthrene I've been using Cygwin and MinGW for 12 years, and had problems surprisingly rarely. The important thing is that if something doesn't work, it's always possible to fall back on Windows tools, or anything — processes can be started in the same way as any other shell starts them.Choriocarcinoma
Okay, your answer is from 2011. Today powershell runs on linux too. And -- my personal opinion -- bash is too antique. The syntax is horrible and I would rather use a different scripting language. Nowadays with python being standard on most linux distros I don't see any reason to use bash for scripts. I'd advise you to check out powershell again as there has happened a lot since 2011.Jaquelinejaquelyn
Link for those curious about PowerShell on LinuxPresnell
C
35

I have used a bit of PowerShell for script automation. While it is very nice that the environment seems to have been thought out much more than Unix shells, in practice the use of objects instead of text streams is much more clunky, and a lot of the Unix facilities that have been developed in the last 30 years are still missing.

Cygwin is still my scripting environment of choice for Windows hosts. It certainly beats the alternatives in terms of getting things done.

Clemenciaclemency answered 21/2, 2009 at 19:59 Comment(15)
I agree about using .NET objects rather than text streams. Using plain text is just so powerful, and makes command chaining so easyHannahannah
Using objects is a paradigm shift and takes some getting used to. But avoids the whole re-parsing at each step where structured data is involved (e.g. no need to ensure fields are delimited).Recidivism
@Andy White @Daishiman, I can understand where there is a learning curve with PowerShell, but piping objects can be much more flexible than piping text. @Recidivism is correct. :)Archibaldo
It's not just the learning curve though. It's hard to argue with the power of plain text. It's hard to argue with 30+ years of tools that deal with text.Hannahannah
Auto makers had a hard time arguing with 1000+ years of horses too. I'm not trying to be flippant. Just pointing out that past success does not remove the potential benefit of innovation.Balderdash
I agree with your point about innovation. I don't really agree with the point that piping objects is more powerful than piping text. If you are piping objects, all of your command-line tools must understand objects. Plain text is the lowest common denominator that all languages can understand.Hannahannah
Objects CAN be more flexible, but that doesn't mean that parsing them is trivial. Yes, with text you have the formatting issue, but awk, sed and perl have mostly solved the problem. What happens when objects don't have compatible methods? Transliterating objects is much more difficult than text.Clemenciaclemency
Another thing: there's nothing you get with MSH that you don't get with Perl or Python, and both, being full-fledged languages, accomplish much more, and since they're more verbose than bash but about the same as MSH (and much more readable), I still don't see the advantage.Clemenciaclemency
@Clemenciaclemency - I totally agree with your points. Dealing with objects or proprietary configuration mechanisms (rather than plain text) is what makes Windows so difficult to deal with on the command line.Hannahannah
@daishiman - The benefit of Objects is that when you want a property, you ask for it - you don't have to parse,guess,cast. I did not understand your point about "what happens when objects don't have compatible methods" - Could you say that another way or give an example of the problem? Thanks.Introrse
The problem is that if you're dealing with a bunch of dissimilar objects you need to introspection to handle their different methods and properties, which means that the only common denominator is text, so you're back to square one, 'cept without without your wonderful usual UNIX text tools.Clemenciaclemency
@Clemenciaclemency - I understand. In practice people have found this not to be a problem but rather a huge advantage. That said, I can see that if you were an expert text parser, this would be a new skill to learn and it might feel unneeded and awkward at first. Again - whatever helps is the right tool.Introrse
I tried to use bash via Cygwin on Windows but the forward vs. backward slash for paths killed it for me. The whole command line ecosystem needs to be working the same or it's frustrating and error prone.Belldame
I thought programmers especially would understand the need for strongly-typed properties and not random perlthon text-parsing where anything can go wrong without you even knowing it before it's too late? ^^Ricketts
The unix tools are so awesome and advanced. But depending on the text output having so many columns and this particular column is what I want is not so great. What if the command changes down the line. Your neat little script gets broken. And it is not as though you cannot do similar text manipulation with Powershell. But you do have the advantage of dealing with full fidelity objects. Powershell may never be as powerful as bash etc. but it did get this aspect right.Ecosystem
A
15

There are lots of great great answers here, and here is my take. PowerShell is ready if you are... Examples:

grep = "Select-String -Pattern"

sort = "Sort-Object"

uniq = "Get-Unique"

file = "Get-Item"

cat = "Get-Content"

Perl/AWK/Sed are not commands, but utilities hence hard to compare, but you can do almost everything in PowerShell.

Amateurish answered 17/9, 2015 at 8:22 Comment(3)
Can you believe the cryptic four-letter commands our ancestors were forced to use?Choriocarcinoma
@EvgeniSergeev All the above are available by default as sls, sort, gu, gi, gc, respectively. Long readable names which tab-complete, and short type-able names in one system. That's user-friendly progress for you.Now
One of the aliases for Get-Content is cat, so for that one there isn't any difference between Cygwin/Unix and PowerShell. Unfortunately, in most cases, Microsoft's documentation for cmdlets is lacking information about the aliases, but a list of all aliases is output by using Get-Alias in a PowerShell session. The alias for "Get-Unique" is "gu", so that is shorter than the Cygwin/Unix one!Breakthrough
E
14

I have only recently started dabbling in PowerShell with any degree of seriousness. Although for the past seven years I've worked in an almost exclusively Windows-based environment, I come from a Unix background and find myself constantly trying to "Unix-fy" my interaction experience on Windows. It's frustrating to say the least.

It's only fair to compare PowerShell to something like Bash, tcsh, or zsh since utilities like grep, sed, awk, find, etc. are not, strictly speaking, part of the shell; they will always, however, be part of any Unix environment. That said, a PowerShell command like Select-String has a very similar function to grep and is bundled as a core module in PowerShell ... so the lines can be a little blurred.

I think the key thing is culture, and the fact that the respective tool-sets will embody their respective cultures:

  • Unix is a file-based, (in general, non Unicode) text-based culture. Configuration files are almost exclusively text files. Windows, on the other hand has always been far more structured in respect of configuration formats--configurations are generally kept in proprietary databases (e.g., the Windows registry) which require specialised tools for their management.
  • The Unix administrative (and, for many years, development) interface has traditionally been the command line and the virtual terminal. Windows started off as a GUI and administrative functions have only recently started moving away from being exclusively GUI-based. We can expect the Unix experience on the command line to be a richer, more mature one given the significant lead it has on PowerShell, and my experience matches this. On this, in my experience:

    • The Unix administrative experience is geared towards making things easy to do in a minimal amount of key strokes; this is probably as a result of the historical situation of having to administer a server over a slow 9600 baud dial-up connection. Now PowerShell does have aliases which go a long way to getting around the rather verbose Verb-Noun standard, but getting to know those aliases is a bit of a pain (anyone know of something better than: alias | where {$_.ResolvedCommandName -eq "<command>"}?).

      An example of the rich way in which history can be manipulated:

      iptables commands are often long-winded and repeating them with slight differences would be a pain if it weren't for just one of many neat features of history manipulation built into Bash, so inserting an iptables rule like the following:

      iptables -I camera-1-internet -s 192.168.0.50 -m state --state NEW -j ACCEPT

      a second time for another camera ("camera-2"), is just a case of issuing:

      !!:s/-1-/-2-/:s/50/51

      which means "perform the previous command, but substitute -1- with -2- and 50 with 51.

    • The Unix experience is optimised for touch-typists; one can pretty much do everything without leaving the "home" position. For example, in Bash, using the Emacs key bindings (yes, Bash also supports vi bindings), cycling through the history is done using Ctrl-P and Ctrl-N whilst moving to the start and end of a line is done using Ctrl-A and Ctrl-E respectively ... and it definitely doesn't end there. Try even the simplest of navigation in the PowerShell console without moving from the home position and you're in trouble.

    • Simple things like versatile paging (a la less) on Unix don't seem to be available out-of-the-box in PowerShell which is a little frustrating, and a rich editor experience doesn't exist either. Of course, one can always download third-party tools that will fill those gaps, but it sure would be nice if these things were just "there" like they are on pretty much any flavour of Unix.
  • The Windows culture, at least in terms of system API's is largely driven by the supporting frameworks, viz., COM and .NET, both of-which are highly structured and object-based. On the other hand, access to Unix APIs has traditionally been through a file interface (/dev and /proc) or (non-object-oriented) C-style library calls. It's no surprise then that the scripting experiences match their respective OS paradigms. PowerShell is by nature structured (everything is an object) and Bash-and-friends file-based. The structured API which is at the disposal of a PowerShell programmer is vast (essentially matching the vastness of the existing set of standard COM and .NET interfaces).

In short, although the scripting capabilities of PowerShell are arguably more powerful than Bash (especially when you consider the availability of the .NET BCL), the interactive experience is significantly weaker, particularly if you're coming at it from an entirely keyboard-driven, console-based perspective (as many Unix-heads are).

Eupatrid answered 1/11, 2013 at 6:12 Comment(6)
You ask "anyone know of something better than: alias | where {$_.ResolvedCommandName -eq "<command>"}?". How about just alias -Definition *property (or any other pattern)? I think the problem with your answer is that you are conflating the shell and the console: remember you have a choice of consoles with different editing options. They deliberately left the DOS console editing broken to encourage people to use other consoles such as ISE.Overstride
BTW, your !! example can be written in Powershell as (h -c 1) -replace '-1-','-2-' -replace '50','51' | iex but it's easier to up arrow and edit for a single command. If you want to do it across a lot of commands I think Powershell would win. To repeat 10 commands ending at command #255 with your edits: (h -c 10 -id 255) -replace '-1-','-2-' -replace '50','51' | iex Also Powershell's history lets you do things unheard of in Linux shells; if you wonder retrospectively just how long a command took to run: h -id 20 | select { $_.EndExecutionTime - $_.StartExecutionTime }Overstride
@Overstride In respect of your comment re the shell and the console--I think that that's just a fundamental difference between Bash and PS; that is: Bash expects to deliver a certain interactive experience whereas PS "leaves" that to something else. I don't have a lot of experience with the ISE console, but from I can remember, it doesn't have a tremendously rich interactive experience either.Eupatrid
@Duncan, yes - good point about the ability of PS to deliver in respect of clever history tricks.Eupatrid
@Overstride ... but despite your assertion that some things are unheard of in Linux shells: fc -e "sed -i -e 's/-1-/-2-/g' -e 's/50/51/g'" 10 255Eupatrid
I wasn't claiming that bit was impossible. To be honest though I would probably write a script if I wanted to repeat a bunch of commands. ISE is pretty good, but poorly documented and needs a different way of working. I only recently found Shift+Enter to enter multi-line commands (or multiple commands as a single history item). It also has snippets, intellisense, colour highlighting, debugger and so on. It doesn't have search (unless I haven't found that yet), but it does allow you to install add-ons so there may be one for that.Overstride
C
8

I am not a very experienced PowerShell user by any means, but the little bit of it that I was exposed to impressed me a great deal. You can chain the built-in cmdlets together to do just about anything that you could do at a Unix prompt, and there's some additional goodness for doing things like exporting to CSV, HTML tables, and for more in-depth system administration types of jobs.

And if you really needed something like sed, there's always UnixUtils or GnuWin32, which you could integrate with PowerShell fairly easily.

As a longtime Unix user, I did however have a bit of trouble getting used to the command naming scheme, and I certainly would have benefitted more from it if I knew more .NET.

So essentially, I say it's well worth learning it if the Windows-only-ness of it doesn't pose a problem.

Coons answered 21/2, 2009 at 20:5 Comment(2)
There is an open source project "Pash" that lets you run PowerShell on other platforms via Mono. tinyurl.com/6dyosoHinny
Whoa! Thanks for the tip; I can't wait to give it a tryCoons
O
6

TL;DR -- I don't hate Windows or PowerShell. I just can't do anything in Windows or on PowerShell.


I personally still find PowerShell underwhelming at best.

  • tab completion of directory paths do not compound, requiring the user to enter a path separator after every name completion.
  • I still feel like Windows doesn't even have the concept of a path or of what a path is, with no accessible user home indicator ~/ short of some @environment://somejibberish/%user_home%
  • NTFS is still a mess and seemingly always will be. Good luck navigating.

  • cmd-esque interface, The dinosaur cmd.exe is still visible in PowerShell, EditMark still being the only way to copy information, and copying only in the form of rectangular blocks of visible terminal space. and EditMark still being the only way to paste strings into the terminal.

  • Painting it blue doesn't make it any more attractive. I don't mind Microsoft developers having a taste in color though.

  • Windows always opens at top left corner of screen. For somebody who uses vertical task bars this is incredibly annoying, especially considering that the Windows task bar will cover the only corner of the window that gives access to copy/paste functionality.

I can't speak much on the grounds of the tools Windows includes. Being that there is a whole set of open-source, freely licensed CLI tools, and PowerShell ships with, to my knowledge, none of them is an utter disappointment.

  • PowerShell's wget takes seemingly incomparable arguments to GNU wget. Thanks, glimmer of hope portably-useless.
  • PowerShell POSIX is not Bash-compatible, particularly the && operator is not handled, making the simplest of conditional command following not a thing.

I don't know man; I gave it a shot, I really did; I still try to give it a shot in the hopes that the next time I open it it will be any less useless. I cannot do anything in PowerShell, and I can barely do things with a real project to bring GNU tools to Windows.

MySysGit gives me the dinosaur cmd.exe prompt with a couple of GNU tools, and it is still very underwhelming, but at last path completion works. And the Git command will run in Git Bash.

Mintty for MySysGit gives the Cygwin interface over mysysgit's environment, making copy and paste a thing (select to copy (mouse), Shift+Ins to paste, how modern...). However, things like git push are broken in Mintty.

I don't mean to rant, but I still see huge problems with command-line usability on Windows even given tools like Cygwin.


P.S.: Just because something can be done in PowerShell, doesn't make it usable. Usability is deeper than ability and is what I tend to focus on when trying to use a product as a consumer.

Odaodab answered 21/2, 2009 at 19:42 Comment(3)
Turn on QuickEdit mode? Select and press enter to copy, ctrl-v to paste, no more Edit->Mark or Edit->Paste. While in the preferences, set the Window position and untick "let system position window". Tab completion is not just completing filesystem paths, it's also completing variable names, command names, object properties, you might be about to type . or [ to access a property or index, so it can't just add a path separator on the end. Which PowerShell wget, exactly? Which PowerShell POSIX? It's not trying to bring gnu tools to Windows, or be bash-compatible btw.Now
Yeah i know it's not trying to be bash. I refrained from saying so in the original post. I would say which w get binary but there is no "which" command in power shell :( i don't recall where buy i read power Shell was supposed to be POSIX compatibleOdaodab
re: "no which" -> (get-command wg*.exe).Path. Re: bash completion and readline -> leeholmes.com/blog/2012/09/13/… leading to github.com/lzybkr/PSReadLineNow
I
6

If you like shell scripting you will love PowerShell!

Start at A guided tour of the Microsoft Command Shell (Ars Technica).

Inky answered 21/2, 2009 at 20:1 Comment(9)
I'm not sure about your conclusion - but the article looks useful. Thanks.Sniff
I don't see how that follows at all. I like shell scripting because it's free, and runs on all my computers. As Stroustrup said, "It will take a lot to persuade me that the world needs yet another proprietary language...especially [one]...closely integrated with a specific proprietary OS".Coadjutant
I love shell scripting and I tolerate PowerShell. It's commands and syntax are atrocious. Horribly long commands and options without any useful command completion. Or if there is I haven't found it. Too many features crammed into single commands that should be separated out. Weird variable and escape syntax only a DOS batch programmer could love.Fain
@Zan Lynx - You are right about not having found stuff. All commands have aliases, and many of them match both DOS and UNIX commands ( ps, dir, rm, ls, kill, history, man, cat, clear etc.) The names are long so that they have a meaningful name. Great for scripts -it helps when a new person has to use and maintain it. There is tab expansion for cmdlets, functions, variable, path, parameters etc. Much of the syntax is from Unix shells and what escape syntax are you talking about? `` is not used for escape since it is the path separator in Windows.Ecosystem
write-output "this is a ""test""". Really, double the character to get two of them. But to output a string without interpreting a variable you use a back-tick. Lack of consistency? Yes. Hard to remember? Yes. Sometimes you have to use a -LiteralPath argument.Fain
@manojlds: And look at this: #528272Fain
@Zan Lynx - Your arguments are not valid. To stop interpreting variables, use ' (single quotes). To the double double quotes thing - same, use write-output 'this is a "test"'. The question you are pointing to is for Regex and the escaping for regex is valid everywhere. Powershell has Here-Strings / verbatim strings as well. Even Java doesn't have these! Try escaping regex in Java. And you don't sometimes use literalpath. You use when you need to. LiteralPath treats wildcard characters verbatim and doesn't expand them. You use it when your files have it. It gives you more options.Ecosystem
@manojlds: Compared to the bash shell, Powershell is full of inconsistencies that make no sense and are just confusing. In bash you use the same escape character everywhere and file paths are escaped just as strings are. You don't need a special parameter for it. If you expand a variable that has special characters in it, you put it in double quotes and the contents are safe, no need to call a function to re-escape it.Fain
@Zan Lynx - There are no inconsistencies. Even write-output "this is a `"test`"" works. Just use ` instead of `\`. regex::escape exists to help you so that you don't miss escaping stuff. It is not necessary to use it. You are thinking that additional options that are there to help you and prevent mistakes are inconsistencies.Ecosystem
E
6

When you compare PowerShell to the combination Cygwin/Perl/Shell, be aware that PowerShell only represents the "Shell" part of that combination.

You can however invoke any command from PowerShell just as you do from cmd.exe or Cygwin. It does not re-implement the specified functions, and it is certainly not comparable to Perl.

It's "just" a shell, but it makes programming easier providing a comfortable interface to the .NET universe.

Also keep in mind that PowerShell requires Windows XP, Windows Server 2003 or higher, which may pose a problem depending on your IT infrastructure.

Update:

I had no idea what kind of philosophical debate my answer would spark.

I posted my answer in the context of the question: Compare PowerShell to Cygwin and Perl and Bash.

PowerShell is a shell, as it makes no syntactic difference between built-in commands, commandlets, user functions, and external commands (.exe, .bat, .cmd). Only invoking .NET methods differ by adding a namespace or an object in the call.

Its programmability derives from the .NET framework, not from anything specific to the PowerShell "language".

I'd say I believe PowerShell is a "scripting language" as soon as Bugzilla or MediaWiki are implemented as PowerShell scripts running on a web server ;)

Until then, enjoy the comparisons.

Erminiaerminie answered 21/2, 2009 at 20:2 Comment(7)
Yeah, I guess when I'm talking about Unix "shell" I'm also referring to all the normal utilities that come with Unix, such as grep, awk, etc. I'm just wondering if PowerShell offers similar utilities out-of-the-box.Hannahannah
Powershell is not "just a shell". It is a scripting language. I wonder in what way it is not comparable to perl? I admit that it isn't as mature, but beyond that I don't see the disparity.Balderdash
@EBGreen, What exactly do you mean with this comment? I agree that any shell or scripting language is inherently similar, but I was wondering more about the specific capabilities of PowerShell vs. bash/perl/other unix shells/scripting languages.Hannahannah
Powershell has an incredibly wide range of functions. It is - A interactive, composable shell - A rich interactive scripting language - A programming language It has a rich set of OO & tesxt utility functions (i.e. equivalents to grep/awk/etc) as well.Introrse
@Andy - I understood Devio to say that powershell is not as powerful as Perl. I don't believe that is true and I just wondered why he thought that was the case.Balderdash
Its a very good point. You will find lots of posts on how to unzip or download a file via http in powershell. While you can do it, that isnt its strength. You are better off calling an exe for that. That of it as batch, but much improved.Bithynia
I would tend to agree with the notion that PowerShell is primarily a shell. I've done some build and deployment scripting in PowerShell, and I've found that as my scripts got larger, PowerShell as a language didn't handle the growth very well. My code got rather clumsy looking and hard to maintain. That said, this may also be true of bash and any other sh derivatives. PowerShell is superior to cmd for small scripts, but beyond something relatively simple, I would prefer something full blown like Python. (I guess I might be "doing it wrong", but a high learning curve is a strike in my book.)Gottwald
C
6

As my recent experiments led me into depths of PowerShell and .NET calls, I must say that PowerShell can replace Cygwin and Unix shell.

I'm not sure about Perl, but since both PowerShell and Perl are Turing complete as programming languages, I give this as a yes to replacing Perl too.

One thing that PowerShell has above Cygwin and ordinary Bash under *nix, is its ability to perform sandboxed DLL calls, manipulating the operating system via direct API calls, WMI methods and even COM objects. How about launching Internet Explorer via code, then doing whatever you want with its displayed document, effectively emulating a back-end for a Web server?

How about gathering data from SQL servers and other data providers, parse them and export as CSV, mail messages, text and actually any kind of existing and non-existing file formats? (With proper skills of creating a valid file out of data received, of course, but CSV are readily available).

And there is an extra security available via signed cmdlets and scripts, group policies, and execution policies that help prevent malicious code from running on your system even if you run them as administrator.

About what commands are implemented - the answer by Richard lists them and PowerShell's capability of emulating their functionality already.

About whether PowerShell is strong to warrant switching over - this is more a matter of personal preference, although as more and more Windows services are providing PowerShell cmdlets to control them, not using PowerShell with these services present is considered a hindrance. (Hyper-V server is the primary such service, and it also provides the ability to do more with PowerShell cmdlets than with GUI!)

Probably this answer is five years late, but still, if someone performs administrative tasks or general scripting of various stuff on Windows, they should definitely try harnessing PowerShell for their purposes.

Cannular answered 17/6, 2015 at 20:25 Comment(0)
L
4

The cmdlets in PowerShell are very nice and work reliably. Their object-orientedness appeals to me a lot since I'm a Java/C# developer, but it's not at all a complete set. Since it's object oriented, it's missed out on a lot of the text stream maturity of the POSIX tool set (awk and sed to name a few).

The best answer I've found to the dilemma of loving OO techniques and loving the maturity in the POSIX tools is to use both! One great aspect of PowerShell is that it does an excellent job piping objects to standard streams. PowerShell by default uses an object pipeline to transport its objects around. These aren't the standard streams (standard out, standard error, and standard in). When PowerShell needs to pass output to a standard process that doesn't have an object pipeline, it first converts the objects to a text stream. Since it does this so well, PowerShell makes an excellent place to host POSIX tools!

The best POSIX tool set is GnuWin32. It does take more than 5 seconds to install, but it's worth the trouble, and as far as I can tell, it doesn't modify your system (registry, c:\windows\* folders, etc.) except copying files to the directories you specify. This is extra nice because if you put the tools in a shared directory, many people can access them concurrently.

GnuWin32 Installation Instructions

Download and execute the exe (it's from the SourceForge site) pointing it to a suitable directory (I'll be using C:\bin). It will create a GetGnuWin32 directory there in which you will run download.bat, then install.bat (without parameters), after which, there will be a C:\bin\GetGnuWin32\gnuwin32\bin directory that is the most useful folder that has ever existed on a Windows machine. Add that directory to your path, and you're ready to go.

Luzluzader answered 26/10, 2012 at 14:52 Comment(0)
C
2

I haven't seen that the PowerShell has really taken off, at least not yet. So it might not be worth the effort of learning it unless those others on your team already know it.

For your predicament you might be better off with a scripting language that others could get behind, Perl like you mentioned, or others like Ruby or Python.

I think a lot of it depends on what you need to do. Personally I've been using Python for my own personal scripts, but I know when I start writing something that I'll never be able to pass it on - so I try not to do anything too revolutionary.

Copperas answered 21/2, 2009 at 20:12 Comment(0)
S
2

Why not use both? Call PowerShell scripts in Cygwin just like any other interpreted scripts like Perl, etc.

I do this enough that I wrote https://bitbucket.org/jbianchi/powershell for a Bash wrapper to call powershell.exe in Cygwin. It can be used as a shebang as the first line of a powershell.exe .ps1 script (since PowerShell also uses "#" as a comment). See https://bitbucket.org/jbianchi/powershell/wiki/Home for examples

Setser answered 25/10, 2013 at 17:1 Comment(0)
S
1

In a couple of lines, Cygwin and PowerShell are different tools however if you have Cygwin installed you can run the Cygwin executables within a PowerShell session. I've gotten so used to PowerShell that now I no longer use grep, sort, awk, etc. There are pretty much built-in alternatives in PowerShell, and if not you can find a cmdlet out there.

The main tool I find myself using is ssh.exe, but within a PowerShell session.

It works great.

Salubrious answered 29/8, 2013 at 7:13 Comment(0)
D
0

I found PowerShell programming to be not worth the effort.

I have several years of experience with shell scripting under Unix, but I found it enormously difficult to do much of anything with PowerShell.

It seems like many functions require you to interrogate the Windows Management Interface and issue SQL-like commands to get the information you need.

For example, I wanted to write a script to remove all files with a specific suffix from a directory tree. Under Unix, this would be a simple ...

find . -name \*.xyz -exec rm {} \;

After a couple of hours dicking around with Scripting.FileSystemObject and WScript.Shell and issuing "SELECT * FROM Win32_ShortcutFile WHERE Drive = '" & drive & "' AND Path = '" & searchFolder & "'", I finally gave up and settled for Windows Explorer's Search command and just do it manually. There's probably some way to do what I wanted, but I didn't see anything obvious and all the examples on the MSDN site were so trivial as to be worthless.

EDIT Heh, of course as soon as I wrote this I poked around some more and found what I had been missing: the -recurse option to the remove-item command is faulty (revealed if you use get-help remove-item -detailed).

I had been trying "remove-item -filter '* .xyz' -recurse" and it wasn't working, so I gave up on it.

Turns out you need to use get-childitem -filter '*.xyz' -recurse | remove-item

Decompound answered 22/2, 2009 at 2:34 Comment(4)
I think you're confusing Windows Scripting Host (WSH) with PowerShell. They're totally different.Obsessive
As an example, if you want to delete all the files that end in .TMN you can issue this command get-childitem c:\ -include *.TMN -recurse | foreach ($_) {remove-item $_.fullname}Obsessive
@Mystere: I tried this, and it didn't seem to work. After some futzing about, it seems that *.tmn is what you need (instead of just .tmn)Magnum
I couldn't respectfully disagree more, and I'm by no means a Windows guy. I find PS much easier to script than bash. PS is more consistent. With Bash, any console utility you invoke has its own syntax, and unique behavior, and the bash syntax itself is rather cryptic. PS is much more robust with language features like anonymous functions, (scriptblocks), parameter validation, advanced functions etc. Plus concepts of modules for portability and many other features. Mainly though, the biggest reason is the thing you always hear about PS, objects trump text. Bash is still faster though.Becka
O
0

PowerShell is very powerful, more powerful than the standard built-ins of the Unix shells (but only because it includes much of the functionality usually shelled out to subprograms). Also, consider that you can write applets in any .NET language, including IronPython, IronRuby, PerlNet, etc.. or you can simply call your Cygwin commands from PowerShell, ignoring all the extra functionality and it will work similarly to Bash, KornShell, or whatever...

Obsessive answered 22/2, 2009 at 3:22 Comment(4)
I think you may be missing the design goals of Unix shells. Not knocking PowerShell (would like to learn more myself) but you need to understand Unix tooling to make a statement like that.Heterozygous
@Xepoch - No, I think you're missing the design goals of PowerShell. PowerShell can do everything the Unix shells can do in the same way that they do it. However, the real power comes when you utilize PS's object piping system rather than just parsing text output. So, PowerShell can do exactly what bash or korn can do, but they can't do what PowerShell can do.Obsessive
I simply don't know PowerShell enough to give you a critique thereon, but as you clearly know the goals of the Unix shells are not necessarily a fully comprehensive replacement of external tooling, rather the control structures around it. Again I cannot at this time compare nor contrast but elevating PowerShell because it has more built-ins is not necessarily the boon of the Unix shells.Heterozygous
@Xepoch - The point is, you can choose which way you want to do it. You can use all the goodness of the built-in shell, or you can ignore and do it the way Unix does it. It's your choice. And choice is good, right?Obsessive
K
0

You can also try running Bash scripts on Windows using BashWin at https://github.com/skanga/BashWin.

Kebab answered 13/7, 2012 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.