I have used bash, zsh, ksh, and now powershell. I am by no means an expert on UNIX shells. I could get all my work done on any one of these. My main shell is Zsh with some oh-my-zsh plugins because I’m lazy. What can you do in powershell that you can’t do in any other shell? Also, does the shell you use even matter? Sorry if this is a dumb question.
Begone, heretic
My current stance is that if your task is complex enough to require the advanced features powershell offers, it should probably just use Python or similar programming languages instead of a shell script. Hell, I’d even argue that is almost exactly what Python is for: it is simple to hack something together while offering powerful stuff in its standard library and common packages.
From an enterprise IT stance I’d disagree. For phython you need an interpreter, while powershell is available on every random windows 20xx server. It is far easier to do this task in PS than requesting the python interpreter to be installed on the machine and then doing it in python.
Unless you are on a Linux box
Yes sure! Unfortunately most companies which I work for (insurance and banks mostly) still use windows quite often…
PS has a lot of utilities built-in, so you can do a lot with it especially if you know it enough. But python is a lot more accessible (and available almost everywhere by default) so yeah.
My favourite for writing ‘advanced’ scripts is Ruby. It’s highly ergonomic for this kind of thing, with its backtick-feature.
Related: https://pyinfra.com/
I went through the comments to check if someone hadn’t already mentioned exactly this. Here you go.
I have used PowerShell since its inception, and still believe that the Monad manifesto is very relevant. However in recent years I have switched to Python for this same reason.
It’s more widely available, especially outside of Windows it already has greater support for advanced features. PowerShell is not a first class contemporary to Python on Linux.
Quite easy to learn. A lot of default learn materials, tutorials, examples.
You’ll also be able to find people in market with Python experience more readily (especially going forward) if you need to hire talent for your company.
Well, it is powerful it can do everything that coreutils do and more. It works with objects instead of strings so you simply can do more with it, it has built in support for json and csv and probably a lot more, it has built-in package manager. It has the worst syntax I’ve seen.
I switched my one windows pc over to nushell, because I couldn’t wrap my head around psh syntax.
The best reason to use PowerShell on Linux is if you are working in a mixed/complex environment, or think that you might pursue a career in enterprise IT and want to learn about working in mixed environments. With PowerShell you can remotely manage Windows systems without having to RDP into them, you can just open a remote PowerShell session and issue commands to the target machine.
I’ve used this to do things like loop through a list of endpoint hostnames, open a session to each one and get an inventory of installed applications on that system and also flag any hosts that didn’t respond to the remote session and then spit out all the results in a .CSV, fully automatic. Run monthly or as needed.
You can also interact with the Windows registry via .NET classes, which PowerShell understands natively because it is .NET. You can do this locally and remotely, which looks something like this:
[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, "<hostname>").OpenSubKey('<registry key path>').GetValue('<key value name>')
So if you wanted to check if a particular registry key was set on a group of Windows machines, you could write a PowerShell script that could just do that for you with no interaction. It’s also possible to modify registry keys remotely with.SetValue('<key value name>')
, so if you need to do that in bulk it’s very convenient.Basically PowerShell can take previously arduous and time-consuming Windows sysadmin tasks and make them scriptable. And not just Windows, anything and everything .NET can be easily managed via PowerShell.
Plus all the add-on modules for Exchange online, Entra etc
It depends what you’re intending to do, but PowerShell objects are really powerful, especially in terms fo searching and sorting. Best example I can give as a starting point, would be feeding in a log file and making each line a PowerShell object. Then use blank characters to make each part of the line a property. Does each line start with a date and time? Cool, now now you can do time based queries, specifying dates, days or time ranges without a bunch of messy regular expressions.
No
Powershell is a Microsoft Windows tool. You will see a lot of new Linux admins bring it over from Windows instead of using native tools. It does run but the functionality is very limited compared to other tools.
Genuine question, but how is functionally more limited? You can still run any executable, munge text outputs, and… Well, isn’t that what just about any shell do?
On top of that, you can play with objects on the pipeline, which fewer shells support.
Edit: perhaps you mean in comparison to scripting tools, e.g. Python? I had read the OP to be asking more about it as a shell, I think.
I got pretty late into the Linux world after having been a Windows admin for years. I got so used to powershell that I still can’t really put together a decent bash script without going over previous scripts or going online for help, but I can still smash out a powershell script easily.
Use what your most comfortable with, but you’ll need bash at some point if you manage Linux servers.
I use PowerShell as my main shell. The reason? The only way that any combination of Ctrl+shift+arrows/home/end works in Linux is by using powershell with vscode terminal. I have tried many other terminals and couldn’t get it to work on bash, there’s always some combination that doesn’t work.
I didn’t use powershell a lot but I remember that something nice about it is that you can use dot.net expression in it.
More than that, PowerShell is itself an implementation of .NET. Powershell “cmdlets” are really just convenient packaging of .NET classes. PowerShell can just handle any .NET functionality you might want to use natively because it is .NET, and then you can work that into standard programming logic like if/while/foreach/etc.
Yeah, you can invoke any dotnet api as long as you know which library to pull it from.
(don’t forget fish, nushell, ion, pdksh, mksh, rc, es, and however many others)
as for picking one, really comes down to personal comfort
if you’re developing shell scripts, then two options to keep in mind:
- POSIX shell (ex. ash, dash) – focusing on portability
- bash – most commonly currently installed shell so access to a broader audience
I forgot about fish. I’ve read it isn’t POSIX compliant. It’s my distro’s default shell but I really didn’t feel like using it so I installed zsh instead. I mostly use Zsh because it’s a lot easier to find unformation about it online. I don’t currently write shell scripts but I may wish to in the not-so distant future. Is Zsh a good choice for that? Thank you!
You can whatever shell you like and still write scripts in bash, as bash is a part of (practically) every system. When you write a .sh script the first line will indicate which shell to use, so you can run bash scripts from any shell so long as you have it installed.
This is the way! I also like using zsh with oh-my-zsh as an interactive shell, but I still write my scripts in Bash. They can run anywhere (and most scripts you’ll find online are also in Bash)
Yup, same. I prefer zsh and omz, but I write all my shell scripts in bash for portability and compatibility.
The #!/bin/bash thing (first line) is called the shebang and it can use any application not just shell, so that means Python or PowerShell or any other tool.
There’s no reason that I know of.
I only use power shell for powercli