13 ms·
Two Months with Powershell on a Unix
- dijit 6y agoDude has more patience than me. I tried running powershell on linux a few times, and every single time the second command I run is not supported on linux. I don't even remember what the commands were, PSremoting was one for sure and I think another was Invoke-WebRequest. Powershell on linux is a nice idea but in the current form it is in name only, as nearly nothing works.
- larntz 6y agoI use powershell on linux occasionally. Primarily with the azure and powercli modules. For doing vmware and azure stuff I can't think of anything that hasn't worked. If you want to do anything that would be windows only then you're right, it probably won't work. That said, I usually have a tmux window ssh'd into a windows machine for powershell. It would be nice to be able to manage ad and exchange from a linux machine with powershell.
- vips7L 6y agoInvoke-WebRequest most certainly runs on linux.
- dijit 6y agoI just attempted to prove you wrong, but it does look like it's fixed. So is creating secure strings. Cool!
- jodrellblank 6y agoCreating secure strings isn't exactly fixed; the type exists but they aren't encrypted in memory on Linux because it doesn't have the DPAPI (Data Protection API) from Windows: https://github.com/PowerShell/PowerShell/pull/9199 https://github.com/PowerShell/PowerShell/pull/9199
- danudey 6y agoThis comment made me go and try out Invoke-WebRequest because I'd forgotten what it was like. Being able to do simple web scraping like this in a "shell" is pretty great: $hn = Invoke-WebRequest https://news.ycombinator.com/ $hn.AllElements | Where {$_.TagName -eq "a" } | Where { $_.Class -eq "StoryLink" } | select "innerHtml"
- vips7L 6y agoIWR and ConvertFrom-Json are what got me hooked on powershell. No more learning complex switches for curl or crazy JQ syntax. It's all built in and makes sense.
- earthboundkid 6y agoFWIW, I already have a command line tool that can do that installed in regular Unix: https://github.com/ericchiang/pup https://github.com/ericchiang/pup The docs even use the same example of scraping HN. How does Invoke-WebRequest decide if it’s looking at HTML or something else?
- jve 6y agoWell, you use Invoke-RestMethod for JSON/XML stuff. Or decide yourself for Invoke-WebRequest to pipe it with whatever the data: ConvertFrom-{JSON,XML,CSV} With Invoke-WebRequest you get back headers, raw response, etc - and you get to decide whats next. With the RestMethod - just the content as an object.
- ct520 6y agoThere is an param to declare response type parsing and if you want to use different parsing. Older versions of powershell use IE and webrequest will randomly barf. It really sucked. You can also use a rest request method that uses json.net to deserialize I believe .
- m0xte 6y agoI’m not sure I could cope with that. I have enough problems with it on windows. Literally every corner you turn something punches you in the balls or turns to dust in your hands. It’s one of the most frustrating things I’ve ever used and I avoid it where I can. I would not invite it onto a *nix machine voluntarily where there are established patterns and solutions which have matured over the space of 40 odd years now.
- munchbunny 6y agoWhere does that happen for you? In my experience with PowerShell on Windows, you have to approach it more like interactive python with shortcuts rather than a regular shell, and once you’re accustomed it works fine. The main issue is that it doesn’t work like a classic shell, it just has some affordances to look like a classic shell in common use cases.
- danudey 6y agoAgreed; it's a lot more like a really weird iPython than it is a drop-in bourne shell replacement with quirks.
- emodendroket 6y agoIt's weird but once you get used to it I think it's cool. No more text munging!
- m0xte 6y agoWell it’s not the concept but the implementation that is at fault. From persistent random WinRM failures, sudden things breaking and the whole thing wrapping rancid bits of WMI and COM which leak through regularly, it’s just pain. On top of that the interface is somewhat flakey and slow. Plus some of the semantics feel like going back to circa 1995 Perl. Oh and don’t get me started on the nightmare that is script signing and security which really does nothing useful other than get in the way. It really feels immature. The biggest travesty is when it is integrated into something else such as ansible, packer, chocolatey etc where it will cost you a couple of days occasionally due to some obscure issue hidden behind even more layers.
- pgsbathhouse2 6y agoInvoke-WebRequest doesn't even work correctly on windows for a large amount of very vanilla web functionality. Powershell is a nice idea, but Microsoft is kidding themselves if they think it's even close to the alternatives.
- bishalb 6y agoCan you give any example? This hasn't been my experience.
- pgsbathhouse2 6y agoYou can literally just go to the list of issues on github and sort by most commented and look at their dates to see they're still relevant issues. This the dumbest type of HN question. You could have discovered it yourself in < 1 minute but you apparently refuse or might just not be able to (of which I sincerely apologize). Also your comment history should warrant a general silence. Fuck you and fuck dang and PG.
- zovin 6y agoSame here. I love using pwsh 7 on windows, but on Linux it was a whole different experience. Granted I was trying to use it on an Arch instance, which isn't technically supported, but I couldn't even get PSGet or PsReadLine to work. However, I was pretty great to run the same scripts cross OS with basic commands like Invoke-WebMethod/Invoke-RestMethod
- jlgaddis 6y agoIf you don't want PowerShell reporting your activities back to the mothership, don't forget to set POWERSHELL_TELEMETRY_OPTOUT=1 in your environment before launching PowerShell!
- htfy96 6y agoNixOS already did this at https://github.com/NixOS/nixpkgs/issues/74516 https://github.com/NixOS/nixpkgs/issues/74516
- amedvednikov 6y agoA shell with telemetry. Insane.
- ct520 6y agoWow... would have never thunk it
- CamperBob2 6y agoNo, it's native 64-bit telemetry. No thunks necessary! This is the new Microsoft.
- ct520 6y agoI see you what you did there! No pun intended on my part, hah.
- waste_monk 6y agoShellemetry, if you will.
- TeMPOraL 6y agoReminds me of a certain package manager with telemetry. Homebrew, was it?
- 6y ago
- nailer 6y agoI've been using Unix for 20+ years, switched to pwsh as my main shell 4 years ago (on both Windows and macOS). Here's my notes: https://github.com/mikemaccana/powershell-profile/ https://github.com/mikemaccana/powershell-profile/ Last few weeks I've been going back to bash, mainly to try out WSL/Ubuntu's defaults, but will probably move to pwsh there too. I like the consistent naming (which means I can guess commands) and avoiding the scraping that's inherent in text-only shells. nushell looks like one to watch too. It's a lot further behind than pwsh but has a faster growing community.
- yoloClin 6y agoHey thanks for this, nushell looks really interesting! I really like the idea of an objective shell but don't like the verbosity of PowerShell inputs. I think if you go through my post history I actually describe wanting something like Nushell, not knowing it existed!
- alkonaut 6y agoTbf most common PowerShell commands have short aliases for interactive use. I like that I can use the long forms in scripting and aliases on command line. For example in my PS (unsure who created aliases because I think some aren't default) I have both "ls" and "dir" for "Get-Childitem", "wget" for "Invoke-Webrequest" and so on.
- figomore 6y agoJust a tip. If you want something like sudo, install gsudo (choco install gsudo). It works like sudo: no new shell, don’t need to use “”.
- jlgaddis 6y agoI installed PowerShell on a Debian machine a while back so that I could use (VMware's) PowerCLI modules / commands. That worked well enough -- exactly as on Windows, AFAICT -- but I can't imagine ever trying to replace my "daily shell" (zsh, in my case) with it.
- 0x8BADF00D 6y agoI like that it passes objects instead of strings, I could see that being really useful. Get-Member will list all methods and properties of an object.
- sedatk 6y agoIt looks like an overhead, but when needed, boy is it a bliss to use.
- sixothree 6y agoMy new favorite recipient of objects is Out-ConsoleGridView (along with the normal gridview). https://devblogs.microsoft.com/powershell/introducing-consoleguitools-preview/ https://devblogs.microsoft.com/powershell/introducing-consol...
- lpghatguy 6y agoAnother shell that has a similar philosophy is Nu[1]. I think the idea of structured shells (and typed shells?) is a very interesting path to improve UX and prevent certain kinds of common error. [1]: https://github.com/nushell/nushell https://github.com/nushell/nushell
- ggm 6y ago"so they do these cool things, which of necessity break a model some of you will have been familiar with for 50 years in some cases, but its amazing" (in my case, 40 years.) There is considerable merit in new things. But there are also costs. And, there is the proplist version of this story in OSX. So, I know this is a "thing"
- jabits 6y agoThere are often more costs in sticking with the same tech for forty years...I would still be coding for DOS...I hope with new generations of devs will come better tools...as well as a greater willingness to move on...
- fapjacks 6y agoWhy? Are you afraid your software is going to wear out on you?
- pcr910303 6y agoWhile I really would like UNIX shells to get the ability to pass structured data, one problem is that most shells out AFAIK there don't give the ability to pass structured data natively. For these structured shells(like Powershell, elvish, Nushell) to succeed, they should... * have a spec on how to pass structured data from a native executable (without function calls like 'from-json') * implement some structured data functionality in all GNU coreutils at the very least * and push the 'structured shell evangelists' to implement these schemes on other programs.
- jodrellblank 6y agoThat is unlikely to happen in PowerShell because the structured data is .Net objects. If you are writing a C# cmdlet, it runs in the PowerShell executable process. That is, the output is not structured text it's live state, open file handles, methods, in-memory structures, whatever you want. You aren't going to do that from a native command running in a separate process returning data through stdout without serializing it - and then you're back to convertfrom-json as far as it matters. > For these structured shells(like Powershell, elvish, Nushell) to succeed, they should... For PowerShell to succeed, Microsoft should rewrite all the GNU coreutils? Did you see the months of outcry when Microsoft used the four characters "curl" in PowerShell? You reckon anyone is going to want unilaterally rewritten coreutils? From Microsoft? Microsoft should stop on their development of a completely new tool and hobble themselves to the constraints of the stuff they're trying to replace, controlled by people they're in competition with? Unlikely fantasy world.
- pcr910303 6y agoFirstly, I wasn't writing the comment specifically about PowerShell - it was just a rant about the structured shells. > That is unlikely to happen in PowerShell because the structured data is .Net objects. Isn't that an implementation detail? > You aren't going to do that from a native command running in a separate process returning data through stdout without serializing it - and then you're back to convertfrom-json as far as it matters. Not having to write 'convertfrom-json' IMHO is a big improvement. 'ls[0..3]' is much more convenient than 'ls | from-json[0..3]'. (Imaginary syntax, BTW.) > For PowerShell to succeed, Microsoft should rewrite all the GNU coreutils? I wasn't really talking about PowerShell either, but well, yes I do think that for PowerShell to succeed there should be support for native PowerShell in 'ls', 'find', 'grep', etc... > You reckon anyone is going to want unilaterally rewritten coreutils? From Microsoft? It doesn't to be 'rewritten', it just have to gain support. I don't think that a set of patches that adds a new output format will be that problematic. And, one doesn't have to use PowerShell support if one isn't using PowerShell. > Microsoft should stop on their development of a completely new tool and hobble themselves to the constraints of the stuff they're trying to replace, controlled by people they're in competition with? Unlikely fantasy world. Er... you know - nobody said that one should replace your bash with Powershell. If you decide that the 30-year-old-unable-to-handle-spaces-in-filenames-by-default script language is fine for you, then you can keep using bash. They're not EEEing... BTW, checkout elvish shell, since it's not from MS and it has some good ideas. You might like it if you're just hating PowerShell b.c. it's from Microsoft.
- louwrentius 6y agoI am dead serious. Powershell should replace (bash) shell scripting under any Linux. I find shell scripts terrible, I've learned to switch to Python quickly, despite the overhead. Powershell is very command-line friendly, with easy tab-completion of commands. I've worked with it extensively on Windows, creating large worklflows with it, it's very nice. Powershell even has a unit test tool called 'pester' which is funny to me because it means 'bully' in my native language.
- geocrasher 6y agoI don't know what your native language is, but in English "pester" means to annoy. A negative connotation no matter what. I don't get the hate on Bash though. It's a natural extension of the command line, and works quite nicely to get a lot done without the overhead. Deployment is a lot easier in my environment (web hosting) because all of the servers are homogenous, and I don't need to worry about any dependencies because I rely mostly on coreutils + grep and awk.
- perl4ever 6y ago>I don't know what your native language is Dutch?
- dntbnmpls 6y ago> I am dead serious. Powershell should replace (bash) shell scripting under any Linux. This probably means you don't have a CS background and/or very little experience with linux/scripting/etc and dare I say powershell itself. Scripting in linux/unix/etc is primarily text based whereas powershell is object based ( which is why powershell tends to be slow in comparison ). Also, the only thing going for powershell is it's tie-in with the .Net Framework along with Microsoft based servers ( such as SQL Server ). Like most Microsoft products, it's meant for their environment. There really isn't any real reason for powershell on linux. > I find shell scripts terrible, I've learned to switch to Python quickly, despite the overhead. Powershell is very command-line friendly, with easy tab-completion of commands. I've worked with it extensively on Windows, creating large worklflows with it, it's very nice. Sounds about right. I can't believe that's what you wrote in support of powershell. Linux, bash, etc was built from the ground up to be command-line friendly. Windows was built to be GUI friendly. But there is a reason why windows administration is viewed as a joke in the tech industry. I guess powershell may seems amazing when going from GUI based administration to actual script based automation/administration. > Powershell even has a unit test tool called 'pester' which is funny to me because it means 'bully' in my native language. Yeah, it means bother/annoy in english, but you probably knew that already.
- xiaq 6y agoShameless plug: if you like the idea of processing structured data in a shell, you might also like Elvish (https://elv.sh/ https://elv.sh/).
- xrd 6y agoThis is the exactly the type of comment I came here for, thanks!
- jodrellblank 6y ago> and expect it to be possible when Powershell 7 comes out (I’m using Powershell 6 while writing this). PowerShell 7 was released in early March a month and a half ago: https://github.com/powershell/powershell/releases https://github.com/powershell/powershell/releases
- Arnavion 6y ago>For example, if we wanted to get the first three files from ls I’d do something like `ls | head -n 3`. In Powershell, it’s `$(dir)[0..2]` since the dir command is returning an array which I can index into. You could do that, but the way to write it in the bash way would be `dir | select -first 3` >a quick Google told me to use `echo | openssl s_client -showcerts -servername joejag.com -connect joejag.com:443 2>/dev/null | openssl x509 -inform pem -noout -text` which works fine in Bash or Zsh. Still, in Powershell, it throws an error for some reason. It won't throw an error (unless you're running it without a tty), but it will ask you to enter the parameter of `echo`. That's because `echo` aka `Write-Object`'s parameter is required, unlike bash's `echo`. `echo |` in bash is the same as `< /dev/null`, but that also doesn't work in pwsh because it doesn't support `<`. The pwsh way is to pipe `$null`, thus $null | openssl s_client -showcerts -servername joejag.com -connect joejag.com:443 2>/dev/null | openssl x509 -inform pem -noout -text
- awinder 6y agoA) this is all good stuff, I want to get better with powershell in 2020 and just learned some things here B) I think the authors point was that a lot of their googling for how to do things in a shell assume posix shell. So copy paste from web doesn’t work as well for powershell, which becomes frustrating for new onboarders.
- jstanley 6y ago> You could do that, but the way to write it in the bash way would be `dir | select -first 3` I would have said "ls | head -n3" as well. I've never heard of "select". I just tried your suggestion and it doesn't appear to work: $ dir | select -first 3 bash: syntax error near unexpected token `3'
- athrun 6y agoIt's because it's a PowerShell syntax. What the parent meant was "here's how to write it in PowerShell so that it still has a 'unix' feel to it (ie: composing commands with pipes and option switches)".
- vic20forever 6y ago
- Rapzid 6y agoOn a related tangent, I think the Powershell project is the only place to get certain DotNet libraries that pertain to remote management. It's a bizarre situation.
- cryptonector 6y ago> For example, if we wanted to get the first three files from ls I’d do something like `ls | head -n 3`. In Powershell, it’s `$(dir)[0..2]` since the dir command is returning an array which I can index into. Hmm, yes, but it's not a lazy array/list, so it's not online, is it. If you have a few million files in a directory, that's not going to work. To be fair, neither is `ls | head -n3` -- you have to use `ls -f | ...` to avoid ls(1) sorting the listing first, but if you do, this will be online. For ls(1) it's not really an issue, but in general you want your shell to be online.
- Arnavion 6y ago>Hmm, yes, but it's not a lazy array/list, so it's not online, is it. If you have a few million files in a directory, that's not going to work. It is lazy. The author slightly misunderstood / oversimplified what's going on. The commandlet does not build an entire array in memory and then write it out; it writes out each item one at a time. Evaluating it as `$(dir)` just forces all its output to be collected into a single value, ie an array. It's the equivalent of writing `output="$(ls)"; <<< $output head -n3` in bash. I posted an alternative way to write this in https://news.ycombinator.com/item?id=22963842 https://news.ycombinator.com/item?id=22963842 which does not need to build the output as an array first and is also more natural to write.
- cryptonector 6y agoThanks for the clarification.
- deleted 6y ago[deleted]
- zenexer 6y agoNote that this article is slightly out-of-date. It looks like the experiment began in January with PowerShell 6, and there are notes about caveats that were scheduled to be fixed with PowerShell 7. PowerShell 7 has since been released.
- klodolph 6y agoI spent some time with Powershell on Windows recently, more time than I am used to. Powershell is a clear winner over CMD.EXE by a mile, but… - As an interactive shell, it suffers from an ecosystem which is much poorer than the bash/zsh ecosystem, - As a scripting language, I’m going to use Python or Go the moment anything becomes larger than a few lines, - Installing Posh Git is just a really mediocre experience (indicative of various, more specific problems) Some of these things can be fixed up, like the bad experience installing Posh Git—you have to update the package manager to a non-release version, but the signature is occasionally broken (it seems to happen again and a gain) so you have to disable signature verification, and then you can use the updated package manager to download the Posh Git package. Through the journey I encountered so many stupid random problems that it’s not something I can just step out and recommend to anyone. I’ll continue using it as my primary shell on Windows, but for various reasons (I forget the exact details) I had to give up on it for working with Git. There was a lot of “action at a distance”, like running ssh-agent would break the “ls” alias for Get-ChildItem, which leaves me high and dry without muscle memory. Meanwhile, Microsoft is also spending time and money on WSL. Again, I’ll still be using PowerShell on Windows but even there it is a damn rough experience. The command-line has been paradoxically a second-class citizen as well as a necessary tool for development on Windows for as long as I can remember, and it will take a lot more improvement to PowerShell before that feeling disappears.
- ct520 6y agoyeah I use to automate a decent amount of stuff for bigger deployments for client with it as it’s easier to pass through security audit then an executable. I always feel like things in power shell are harder then they need to be that other scripting languages I don’t have that problem. One thing I do like is being able to leverage .net locally and also import DLLs. That helps me get around a bunch of gaps.
- beart 6y agoMy biggest issue with powershell is how undiscoverable it is. Good luck doing anything without searching for commands in a web browser. But maybe that's just a result if it being so new. 'ls' has been around for almost 50 years... What it really feels like is a complete programming language inside of notepad.exe because of the historically terrible windows command prompt.
- pvtmert 6y agohere is my opinion on `When X is going to replace bash?` The story goes on like this: You went to DataCentre, logged in to an internal machine, something happened (eg failure, diagnostic necessary et.al.) which you need to fix. given condition, you don't have internet connection inside (air-gapped) - You most likely have man-pages installed and having examples what to do in them, - `--help` yields some sensible output - Shell is 'fast', errors are well-known and defined. Also, take into account of other comments that are discussing `how to do x` and why it doesn't/wouldn't work on PowerShell
- pjmlp 6y agoGiven that example, good luck trying to log into a DataCenter that still uses classical UNIX with either sh or ksh as their only options. Bash also has not replaced all other UNIX shells.
- alkonaut 6y agoThe problem with powershell on Unix/Linux is that there is an implicit assumption everywhere that there is a shell available. If you look at simple git StackOverflow questions like "how do I get the oldest common ancestor commit for two branches", you'll happily see people answer with combinations of shell commands that assumes that there is a sh (or some times even bash or zsh!). Absolute madness that git can't do basic things without relying on external applications that may or may not exist.
- bragh 6y agoIt's not madness, it's the price of flexibility and one that I am personally gladly to pay. I am forced to use Windows and pre-approved tooling daily at work, you can't imagine what a relief it is to use Linux at home, where you can chain together the exact programs that you want and make them work the way you want.
- alkonaut 6y agoI don’t mind that programs provide output that can be chained and so on, but what annoys me is when portable (cross platform) apps m, like e.g git, doesn’t provide core functionality without the assumption of a specific environment. It's fine to let the user sort the outputs using an external program. But if the sorting is so commonly required that it's a "core feature" then perhaps the program itself should do it. Since most developers use windows, most windows users use cmd as their shell and git is the most common vcs around, it wouldn’t surprise me if the most common environment for git is ... Windows cmd. Where there is no sed/grep/awk etc!
- sixbrx 6y agoI'd suppose most WIndows users using git either use it through a UI (e.g.Intellij's), or else in the bash shell for Windows that comes with the git package for windows. Starting that bash shell is easier than mucking with the path and the old cmd.exe is awefull in so many ways, anyway.
- beagle3 6y agoOne of the missing ingredients in PowerShell when I tried to use it was network transparency - which I suspect will be hard to solve since it passes .net objects around. It is not often that I pipe things into or out off ssh, a Unix domain socket or a pipe, but when I do it’s usually significantly easier and more efficient than other ways. I also often save outputs rather than pipe, to inspect/debug, or avoid re-computation. I last played with PowerShell on Win7 ages ago so things might have improved dramatically (e.g. if every output is guaranteed to round trip serialize with a network stream).