I'm interested now that PowerShell is cross platform. For someone reasonably fluent in bash/zsh (and Python, etc.) already, could someone give examples of where they might use PowerShell?
http://nushell.sh/ is an example of a new (non-POSIX) shell that's trying to be more structured, similar to PowerShell. Thanks to nu's new -c option, it's possible to use nu profitably even if you don't make it your shell. I doubt I'd ever use PowerShell as my system shell on Mac/Linux, but I don't want to close myself off from tools that might be useful for particular tasks or scripts.
I like to use PowerShell for scripts (sometimes), while I use fish as a interactive shell. I'm a SW developer familiar with .NET, and it's reasonably easy to call any .NET functions from PowerShell while it's also easy to call any external program (do pipes and all this unixy things). Mostly, my use cases are things other people would use Python rather than Bash. PowerShell is a compromise between the two, in my view.
For example when I wanted to create and keep btrfs snapshot with exponential decay on their frequency, that would be quite tedious to write in Bash (I think, at least).
I used to like that I can run the same thing on Windows and Linux, but I stopped using Windows and got used to programs that would have to installed on Windows, so it's no longer an advantage to me. But in theory, it's still IMHO easier to run PowerShell script on Linux than it's to run Bash scripts on Windows.
I have not seen the nushell, thanks for posting that! From PowerShell I'd expect more online material and more stability.
It also has quite a bit of disadvantages:
* Sometimes it's called `powershell` and sometimes `pwsh`, breaking shebangs
* Startup is sloow. May be issue for some scripts that run often.
* Linux people sometimes look at me very strangely :D
* Installation size is significant if you count .NET into that. So not a good fit for setting up docker containers :/
(btw, it works on Linux for quite some time already)
> Sometimes it's called `powershell` and sometimes `pwsh`
This probably isn't something kbd would run into; the command is always `pwsh` on macOS/Linux.
Windows users get `powershell` (Powershell 5) and `pwsh` (Powershell Core 6 and Powershell 7) for backwards compatibility reasons. It's another `python` vs `python3` situation.
> For someone reasonably fluent in bash/zsh (and Python, etc.) already, could someone give examples of where they might use PowerShell?
My experience as a regular bash user and very occasional PowerShell one has been that I can write a script much more quickly using bash, but it has a better chance of getting things right first time using PowerShell. Generally this is because of simple things like the fact that arguments are passed in proper data structures rather than as quoted strings.
So I like PowerShell, it's effective and pragmatic and I think it's potentially good for scripts you want to put in production or redistribute. There are just a few problems -
* As an occasional user I find it all a little hard to keep track of, with liberal use of aliases and abbreviations (obviously intended to simplify things, but they mean reading examples is more complicated) and a less regular syntax around things like piping
* I believe that pipelines will (sometimes? often? always?) pass an in-memory structure rather than streaming - this can make it a lot less scalable (would appreciate correction on this but I do recall running out of memory on things I wouldn't have had a problem with in a Unix shell)
* The fact that it isn't "available by default" does matter, really - after all this is one of the biggest reasons one writes a shell script in the first place, rather than using some other language
Besides devops, I also do sysops for the servers of my team. Backup scripts, scripts for monitoring (plugins for NSClient++) and various utility scripts are written in Powershell.
Other scripts include synchronization of credentials from CyberArk Vault to Keepass, a start-up script which opens my common used applications and VPN (Outlook, Firefox, Visual Studio, OneNote, etc).
I also use it daily during software development if I need to manipulate or transform data I need during development.
I use pwsh as my main shell on both Windows and Linux. For me it's just more sane than bash with less pitfalls that I've experienced. And if there's something I can't do in pwsh, I have the full power of the .net CLR to do it for me.
I have been using pwsh on Linux fairly regularly, too. I got plenty of script and C# modules that I have been using to automate some tasks. (Data wrangling, scraping, and merging, etc.) that I want to use both on Windows and Linux.
Since there are certain tasks that are more suitable with regular shell, I have Ctrl-Alt-P assigned to launch pwsh, and Ctrl-Alt-T assigned to zsh under Linux.
You've probably seem the widely circulated terminal colour test scripts written for Unices and Linux-based operating systems, such as 256colors.pl written in Perl and distributed with XTerm. This is a PowerShell approach. I wrote it to test what Windows Terminal could do.
It exemplifies several things.
It exemplifies the rapid prototyping nature of PowerShell. Development of this script went through several (unpublished) iterations, much of which was tested interactively. Doing that with Edit-Compile-Debug in, say, C would have taken longer.
It exemplifies that PowerShell is not limited to either TUI or GUI. The script both writes stuff to a Windows console and displays a GUI window with buttons.
It exemplifies the runtime library that one gets with PowerShell; here using Forms classes, the String class (including .NET string formatting), and of course the Console class. One gets the .NET (Core) Framework, basically, with which (on Windows) one can do things as varied as rewrite XML files, execute SQL, and (using its .NET API) enqueue builds on a TFS server.
In a very trivial way, it even exemplifies script blocks as lambdas (in the {Click} blocks being passed to the event handler registrations).
There's a whole lot more that it does not exemplify of course; including stuff like the -f and -is operators, hashes and arrays, PSCustomObjects, the object pipeline, ForEach-Object and Where-Object, nullable variables, and the extra standard streams.
And it really only tangentially, with the test at the top, illustrates how PowerShell is akin to TCL, LUA, et al., in that it can be (and is) embedded with different front-ends. In this case, the test ensures that the script isn't accidentally run in PowerShell ISE, which isn't a useful place to run the script, but more importantly which handles its console I/O really slowly to the point that it's actually difficult to interact with the UI. (I didn't employ a similar check for Visual Studio, whose embedded PowerShell has a console UI that is zero rows high.)
The only time I'm forced to use PowerShell is when dealing with edge cases involving MS products. In our case, we needed to refresh SQL Server Cube data within an Excel file and we didn't want to use iron Python, so we relied on PowerShell for doing the refresh work while Python controlled its process.
Personally, since MS has made Javascript a 1st class citizen on Windows now, it would be nice if Javascript and Typescript were better integrated into PowerShell. It would be nice to have one language to rule them all. I hate the time wasted with context switching. Also, I still hate using PS script. They make a lot of strange style decisions like with method / parameter declaration.
Please no. They already compromise a lot on the design of UWP to support JavaScript. Also, JSX was supported for decades on Windows and I don’t think it got any adoption.
The syntax however should get a lifting, to result in something like C# but with less curly braces.
Active Scripting was the Powershell before Powershell. Not in a sense that it's a shell scripting language, but in a sense that it's what Windows admins used to wrote scripts more complicated than a batch file.
That thing supports VBScript and JScript out of the box. Most scripts are written in VBS in practice, though.
I find it fantastic for running scripts on multiple machines. The invoke-command cmdlet makes it very easy to do this if your computers are a domain or have the same username and password. Credentials can also be supplied but that's more cumbersome.
I find many things to be easier than CMD/bash such as looping or chaining commands. I'd imagine nushell has similar benefits though.
Microsoft products such as Azure and AD are easily administered as well.
Do the Office 365, Active Directory, etc modules actually run on Linux or MacOS now? Last time I tried Powershell on my Mac, it couldn't do any of that stuff, and I felt like it was pretty useless to run on anything other than Windows.
With all of their embracing of Linux these days, too bad MS didn't have the balls to integrate bash into Windows back then instead of coming up with something that only Windows users would be using.
I use PowerShell mostly for pulling data from Active Directory. Used to write little C# programs for this but PS has a nice AD module that covers most of the common tasks I need to perform.
The ecosystem of stuff is the first thing that comes to mind. Windows has never been an environment that was well suited for modern automation practices (infrastructure as code).
For instance, it's not straightforward to take a database or XLS document of users and transform that into an Active Directory implementation.
Normally you would need to run this on a Windows machine ... but now I am excited at the thought of using it from my mac.
I'm in the middle of doing a rollout of new network infrastructure, VPN, Firewall appliance, Hyper-V hosts, Active Directory, O365 sync, File storage, etc... for small/medium sized business. I am primarily a software/ops consultant, so I like to build infrastructure 'as data' and then use that data to assert the real thing. IE, terraform apply.
tl;dr – with cross platform we're closer to bringing modern ops tools into Windows land which is exciting. WSL is useful when you are on a Win box, but if you are trying to build infra from Linux/macOS you are struggling.
Here is a big list of powershell crap I have starred recently on Github:
I’d only consider PowerShell when working with tech that is best managed through it, so stuff from Microsoft. It was great for the couple years I used it full time, even built a test runner that used .NET TCP classes to automate stuff through a telnet connection.
Now that I’m on Linux and doing open source stuff, I’d never consider using PS.
- On Linux you have to learn bash anyway, because in the normal course of work you run across bash scripts everywhere.
- Everything on Linux is designed around the Unix philosophy and Unix tools, so PowerShell will feel out of place, and it'll be harder to find examples online. Also, Linux stuff is all configured via text files anyway, so PowerShell's object model is much less enticing.
- The shell's programming environment isn't that important because your scripts shouldn't be that long -- beyond 100 lines or so, you'd be way more productive using an actual programming language like Python instead.
I used PowerShell on Windows for 4-5 years professionally and it was great (finally ditching command.com was a revelation). Now, I've been using Linux, bash and stuff for about 3 years, and it's great. I guess I prefer the Linux environment because it feels tighter and faster.
http://nushell.sh/ is an example of a new (non-POSIX) shell that's trying to be more structured, similar to PowerShell. Thanks to nu's new -c option, it's possible to use nu profitably even if you don't make it your shell. I doubt I'd ever use PowerShell as my system shell on Mac/Linux, but I don't want to close myself off from tools that might be useful for particular tasks or scripts.