6 ms·
Yep, totally agree. Never thought PowerShell would become my go-to utility knife, but here we go.
by MrGilbert 4y ago
Yep, totally agree. Never thought PowerShell would become my go-to utility knife, but here we go.
- deafpolygon 4y agoSame. Story time: I've been a diehard bash/zsh fan for the better part of the last two decades (I was a regular user of it since 1999 so that's 24 years now). I daily-drove some form of Linux since 1999 up until ~2010 when I switched to MacOS (zsh). I only switched to using Windows as my daily driver for ~2 1/2 years ago. Around 2 years ago, I had a script in python that I wrote to process my directory of media files based on the directory name and looked for a subtitle file and pulled that down. For fun, it was a simple enough script that I re-wrote it in bash (WSL2). Six months later, I needed to make a change and was surprised at how much I couldn't read it. I just forgot. That was OK, I had a boring weekend and rewrote the whole thing in PowerShell. A few things I noticed: - It was more LOC owing mostly to brackets, etc, but I'm over the whole "less LOC is better" mentality. - It was faster - but this is probably the result of WSL2, but also, I didn't have to call out to other CLI utilities as much. You can do just about everything in PowerShell and call out to .NET for anything more complex. - It was far more readable. Having to parse while [ ]; do ... done; is harder than ForEach-Object ( ) { } - Passing things around as an object is a very pleasant way of scripting (Python could do the same, but it feels more "Programming Language" than "Scripting" at this point) - Calling an CLI utility is just the same as in Bash (Unless it has spaces, then you just do & "Path to CLI.exe") I had to make a change recently and was pleasantly surprised at how much I could remember the layout, functions, etc. I also tried to adhere to the MS-isms like using a Verb-Noun for my functions where it makes sense, and breaking things up in modules. I don't absolutely love PowerShell, but it is pleasant to use and pulling in .NET stuff is just a line of code away. Now I use it directly as a daily driver, with Windows Terminal - which is also surprisingly pleasant to use.
- delta_p_delta_x 4y agoI want to add: PowerShell has the amazing pipeline operator `|`. Examples abound in Microsoft’s documentation[1][2]. [1]: https://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/04-pipelines?view=powershell-7.3 https://learn.microsoft.com/en-us/powershell/scripting/learn... [2]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines?view=powershell-7.3 https://learn.microsoft.com/en-us/powershell/module/microsof...
- thesuperbigfrog 4y agoPowerShell's pipe silently mangles binary data by default: https://brianreiter.org/2010/01/29/powershells-object-pipeline-corrupts-piped-binary-data/ https://brianreiter.org/2010/01/29/powershells-object-pipeli... Newer versions of Powershell have flags that must be explicitly passed to fix this, but it can bite you if you are not aware: https://stackoverflow.com/questions/54086430/how-to-pipe-binary-data-in-powershell https://stackoverflow.com/questions/54086430/how-to-pipe-bin...
- deafpolygon 4y agoThat is just a case of people trying to use PowerShell as a bash shell combined with the unfortunate decision by whomever at Microsoft to alias PowerShell cmdlets to well known UNIX tools. It doesn't "silently mangle" binary data - it assumes you're piping PS objects or text streams through. If it is a text stream, then it will pipe through as Unicode by default. Invoke-WebRequest (curl) pipes out an object, with the content in (object).Content. Using | uses Unicode by default which "mangles" binary output. So it tries to convert the object into a Unicode text stream then into the file. The correct way to save an image would be to simply set -OutFile (-o) in Invoke-WebRequest. If you MUST pipe it through, then you need to access the object's .Content, then use Set-Content -AsByteStream (for PS7+) or use Set-Content -Encoding byte (PS5) PS5> $(Invoke-WebRequest -Uri https://www.w3schools.com/html/w3schools.jpg https://www.w3schools.com/html/w3schools.jpg -ContentType 'image/jpg').Content | Set-Content -Encoding byte test.jpg PS7> $(Invoke-WebRequest -Uri https://www.w3schools.com/html/w3schools.jpg https://www.w3schools.com/html/w3schools.jpg -ContentType 'image/jpg').Content | Set-Content -AsByteStream test.jpg This is useful if you wanted to put the image inside a variable (rather than create a temporary file for the image) and pass it around until you figure out what you want to do with it.
- thesuperbigfrog 4y ago>> That is just a case of people trying to use PowerShell as a bash shell combined with the unfortunate decision by whomever at Microsoft to alias PowerShell cmdlets to well known UNIX tools. >> It doesn't "silently mangle" binary data - it assumes you're piping PS objects or text streams through. If it is a text stream, then it will pipe through as Unicode by default. The problem is that PowerShell uses aliases that match UNIX command line utility names and the same pipe syntax, but exhibits subtlely different behavior without any indication of the difference. If someone is not aware of the difference and they use PowerShell to process production data, their data could be corrupted without them realizing it.