Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



You can even simplify your `Where-Object` as

    ... | Where-Object Count -gt 1 | ...
Or, of course,

    ... | ? count -gt 1 | ...


...and how do I get a "manual page" for the command "Get-ChildItem" in the console?


You 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.


Get-help get-childitem


Also 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: