4 ms·
PowerShell is more verbose than Bash, and has its idiosyncrasies (like sometimes automatically unwrapping 0/1-element arrays into scalars when you least expect
by Kwpolska 2y ago
PowerShell is more verbose than Bash, and has its idiosyncrasies (like sometimes automatically unwrapping 0/1-element arrays into scalars when you least expect it), but it is more productive and more readable. Object-orientation can be nice to produce pipelines.
For example: find potential file duplicates in a folder, recursively, by grouping by file size:
Get-ChildItem -File -Recurse | Group-Object -Property Length | Where-Object { $_.Count -gt 1 } | Sort-Object -Property Count
No need to remember arcane `file` incantations, no need to parse textual output. You get real objects with properties, and tab completion can see their structure.
Need to parse JSON? No need to involve `jq`, just `Get-Content -Raw whatever.json | ConvertFrom-Json` and do it directly from PowerShell. Need to convert XML into CSV? ConvertFrom-Xml (or Select-Xml) -> do stuff -> ConvertTo-Csv.
Is `Get-ChildItem` too much typing? Use `gci`, or `dir`, or `ls` (by default only on Windows). Is `Where-Object` too much? Try `where` or `?`. And things are case-insensitive.
- g15jv2dp 2y agoYou can even simplify your `Where-Object` as ... | Where-Object Count -gt 1 | ... Or, of course, ... | ? count -gt 1 | ...
- akira2501 2y ago...and how do I get a "manual page" for the command "Get-ChildItem" in the console?
- Miner49er 2y agoGet-help get-childitem
- TeMPOraL 2y agoAlso remember to check Get-Help Get-Help. Also, unfortunately, you'll probably want to Update-Help first, because for reasons beyond my comprehension, PowerShell does not ship with detailed help installed by default.
- BanazirGalbasi 2y agoYou can use "man <command>" the same as in nix. The man command is an alias for Get-Help and it retrieves the same information. There's a lot of nix aliases for Powershell commands. Get-ChildItem is also called with ls, mv calls Move-Item, cd calls Set-Location, etc. They made at least some effort to make it more accessible to people coming from *nix or cmd.
- deleted 2y ago[deleted]