5 ms·
Most of those features are in PowerShell, especially version 7. It uses strict ('reliable') argument passing, strong typing, etc.. It has a bazillion independ
by jiggawatts 5y ago
Most of those features are in PowerShell, especially version 7.
It uses strict ('reliable') argument passing, strong typing, etc..
It has a bazillion independent streams such as "Debug", "Verbose", "Warning", "Error", "Progress", "Output", "Information", etc...
It has "foreach -parallel", which is a fun way to make the CPU fan spin up.
> "Recurse through this directory, while ignoring .git and vim backup files". Read this file into an array, splitting by newline, in a single line of code. It's tiresome to implement that kind of thing in every script I write. At the very least it should be simple and comfortable.
$lines = dir -Recurse -Exclude *.git, *.vim | Get-Content
Not exactly rocket science in most shells? In most shells you can make this a modular function that takes in a list of file names, but PowerShell's "dir" doesn't output file names. It outputs file objects, so then it is just one step to do a filter on something like extension (or length, or whatever):
dir | ? Length -lt 1MB | ? Extension -in @( '.cs', '.rs', '.cpp' )
In PowerShell you can create functions that integrate with the object-oriented pipeline. You can pipe full objects into a function, and objects out. Object attributes can be mapped automatically to function arguments too. You can have begin/process/end blocks so you can do things like open a DB connection, pipe in data, then close the connection neatly without needing to do this all manually each time. Literally something as trivial as this:
Get-Stuff | Out-DatabaseTable -TableName 'stuff_log' -Server '...'
Seriously. Give it a go. It works on Linux too, and it's more unix than unix.
- dale_glass 5y agoPowerShell is indeed excellent, but there's nothing wrong with it having a bit of competition. Some of it is also less than ideal, but rather less than bash. Issues in powershell I can think of: * It leaks environment variables. Set an env var, and it propagates to the shell from which it was called from! * It can't comfortably import environment from batch files. Working with vcvars is an annoyance. You'd think somebody at Microsoft would have made built in support for that one. * Argument passing in Windows is a bloody horror. PowerShell accepts commands encoded in base64 because it sucks so much! * For some reason you can't quote the command itself. You can do: git checkout "blah" But for some reason can't do: "C:\Program Files\Git\bin\git.exe" checkout "blah". Granted, that can be worked around with & or Start-Process, but what a pointless annoyance. That said, I've not really tried it on Linux yet, got to give it a go.
- majkinetor 5y ago> It leaks environment variables It is easy to show that it doesn't: $env:x=1 pwsh $env:x=2 exit $env:x # shows 1 > It can't comfortably import environment from batch files Nor bash can import vars from xonsh or whatever. BTW, there are ready available scripts that can do that, CBB to find it. > Argument passing in Windows is a bloody horror. What? > For some reason you can't quote the command itself. The reason is that its taken as literal string as per semantics of pwsh. Just add dot before it: ."C:\Program Files\Git\bin\git.exe" checkout "blah" In this particular case its because of space in directory name, you could do: C:\Progra~1\Git\bin\git.exe checkout "blah"
- dale_glass 5y ago> It is easy to show that it doesn't: Here: C:\Users\User\Documents> type .\test.ps1 $Env:TESTENV = "Hello" C:\Users\User\Documents> echo $Env:TESTENV C:\Users\User\Documents> .\test.ps1 C:\Users\User\Documents> echo $Env:TESTENV Hello > What? https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-winmain https://docs.microsoft.com/en-us/windows/win32/api/winbase/n... Unlike sane architectures, on Windows, WinMain gets the entire command line as a single string, which means it's each process individually what breaks it into separate arguments.
- majkinetor 5y ago> Here Well... there is still no leak as there is no new shell spawn. I personally find this behavior way more natural then that of bash. If you don't want it, use variables. I can imagine coming from bash background that this irritates you. > WinMain gets the entire command line as a single string This has nothing to do with PowerShell which imposes its own parameter parsing standard so that no individual scripts do that. Also, when we are at it, I guess this "unsane" architecture is more flexible.
- dale_glass 5y ago> Well... there is still no leak as there is no new shell spawn. I personally find this behavior way more natural then that of bash. If you don't want it, use variables. I can imagine coming from bash background that this irritates you. It's a serious annoyance when you're doing build scripts that do stuff like setting $PATH. Suddenly, stuff breaks randomly depending on what you ran in that particular powershell window before. And there's a length limit too, so if you append to a variable you may find stuff breaks on the 5th invocation of the script. It also has the annoying "feature" of leaving you in the last directory the script changed to. Most annoying for debugging stuff. > This has nothing to do with PowerShell which imposes its own parameter parsing standard so that no individual scripts do that. It's not specifically powershell related, but if you're running on Windows, the Windows design problems apply to everything, including powershell. > Also, when we are at it, I guess this "unsane" architecture is more flexible. And more failure prone. It may mean there's no way whatsoever to pass a given path to a program. If a program internally doesn't handle quoting and spaces right, you're screwed. It can also mean security issues. No matter how well you write your code, a malicious party can give you a filename like "data.txt /deleteeverything ", and the program you're calling may choose to interpret that as an argument, regardless of any quoting you might do. For that matter, if you want to have the same flexibility on Unix, you can just concatenate all of argv, and then do your own parsing.
- paskozdilar 5y ago> it's more unix than unix. Would you please elaborate what exactly do you mean by this?
- dale_glass 5y agoPowershell improves on Unix commands being based on text streams, and makes them based on objects. Which means you're pretty much never extracting stuff with cut and awk, and instead can just get whatever field you want. Eg: C:\Windows> Get-AuthenticodeSignature .\explorer.exe Directory: C:\Windows SignerCertificate Status StatusMessage Path ----------------- ------ ------------- ---- BBD2C438000344F439BFDFE5ABAC3223357CD67F Valid Signature verified. explorer.exe C:\Windows> (Get-AuthenticodeSignature .\explorer.exe).SignerCertificate Thumbprint Subject EnhancedKeyUsageList ---------- ------- -------------------- BBD2C438000344F439BFDFE5ABAC3223357CD67F CN=Microsoft Window… {Windows System Component Verification, Code Signing} C:\Windows> (Get-AuthenticodeSignature .\explorer.exe).SignerCertificate.Subject CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US C:\Windows> This of course works with pipes: C:\Windows> Get-ChildItem . | Where-Object { $_.Length -gt 1000000 } | Sort-Object Length Directory: C:\Windows Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 8/11/2021 1:56 AM 1075712 HelpPane.exe -a--- 1/15/2018 12:40 PM 2856800 RtlExUpd.dll -a--- 4/25/2022 1:17 PM 5122632 explorer.exe Isn't that awesome? One of the nice things about it is that you can add stuff easily. You don't have to worry that every script that parses the output of your command will now break because you added an extra column or added extra functionality.
- majkinetor 5y agoYes it is. Its not awesome however to write Where-Object and Foreach-Object instead ? and %. It really turns one down. I can understand fanboyism for not using other aliases but those 2 are special and not using them brings the wrong picture about the pwsh.