5 ms·
Today I appreciated Powershell
by andy81 5y ago
Today I appreciated Powershell
- jmholla 5y agoCan you expand on that? I've never had trouble leveraging xargs and find it aligns well with shell piping.
- bialpio 5y agoNot OP but to me the best thing about PowerShell is that it recognizes that text is not always the best way to output results from commands if you care about creating pipelines. In short, it passes objects around so there's no need for parsing text.
- bialpio 5y agoTwo examples from the article translated into PS (sorry, I'm a bit rusty so the second one may not be the shortest possible): PS> "alice", "bob" | echo PS> Get-ChildItem . -Include "*test.cpp","*test.py" -Recurse | foreach { Remove-Item $_.Name } No text parsing in sight, and the object attributes can be tab-completed from the shell (e.g. I tab-completed the `$_.Name`).
- andy81 5y agoThanks, we were thinking of the same thing.
- Arnavion 5y agoYou don't need to `foreach { Remove-Item $_.Name }` because Remove-Item can take the objects returned by Get-ChildItem directly. Also, expanding the regex into `-Include` parameters is somewhat cheating since `-Include` only takes globs, and it just so happens that that particular regex can be converted into globs. The general equivalent is: gci -re | ?{ $_.Name -match '.*_test\.(py|cc)' } | ri (I used the shorter aliases because someone will probably read yours and reinforce the stereotype that PS is overly verbose.)
- bialpio 5y agoThanks, this is definitely closer to the original use of `egrep`! As for aliases, I prefer long forms because I don't need to think what the seemingly random collections of letters mean, and tab-completion / PowerShell ISE makes it mostly a non-issue when writing.