3 ms·
What makes powershell's system better? I've never used it
by CaptSpify 11y ago
What makes powershell's system better? I've never used it
- TeMPOraL 11y agoYou pass around .NET objects instead of plaintext. Which means information you work on retains its metadata even after lots of piping, and you don't have to invent ad-hoc sed/awk-based microparsers that will break on another machine anyway (be it because of different locale or different command implementation). You operate on objects, and in the end - if you're printing to e.g. terminal - you decide how to display the data. A simple example - equivalent of ls -la with sorting by size: Get-ChildItem | Sort-Object length. You can sort like that because each entry returned by Get-ChildItem is in fact an object. In case of files, it's an object of type System.IO.FileInfo. Some of available properties are: Attributes Property System.IO.FileAttributes Attributes {get;set;} CreationTime Property datetime CreationTime {get;set;} CreationTimeUtc Property datetime CreationTimeUtc {get;set;} Directory Property System.IO.DirectoryInfo Directory {get;} DirectoryName Property string DirectoryName {get;} Exists Property bool Exists {get;} Extension Property string Extension {get;} FullName Property string FullName {get;} IsReadOnly Property bool IsReadOnly {get;set;} LastAccessTime Property datetime LastAccessTime {get;set;} LastAccessTimeUtc Property datetime LastAccessTimeUtc {get;set;} LastWriteTime Property datetime LastWriteTime {get;set;} LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;} Length Property long Length {get;} Name Property string Name {get;} BaseName ScriptProperty System.Object BaseName {get=if ($this.Extension.Length -gt 0){$this.Name.Re... VersionInfo ScriptProperty System.Object VersionInfo {get=[System.Diagnostics.FileVersionInfo]::GetVer... You can sort by any of these. And each object has even more methods available, like Delete(), Encrypt() or SetAccessControl(). Examples of formatting: https://technet.microsoft.com/en-us/library/dd347677.aspx https://technet.microsoft.com/en-us/library/dd347677.aspx.
- CaptSpify 11y agoComing from Unix, this seems super unintuitive, but that's probably just my background. Do you find it tough to figure out what you need to do?
- TeMPOraL 11y agoYou have to learn a few things, just like you have to learn them on Unix. You need to familiarize yourself with some basic cmdlets like Get-ChildItem or Format-Table or Sort-Object, etc. Just like you need to learn about ls or grep or the test syntax under Unix. PowerShell is pretty discoverable though (not as discoverable as one may be used to in Lisp ecosystem, but still). For instance, I can fetch the list of properties like the one I pasted in my previous comment by using: Get-ChildItem somefile | Get-Member. It's roughly equivalent to ls filename | <something that would describe the file's properties if the result wasn't plaintext>. Personally, I do find cmdlets a bit cumbersome to type in even with TAB-completion, but fortunately PowerShell defines a lot of aliases - like (again, I'm just copying what I found after typing "alias"): CommandType Name ----------- ---- (...) Alias cat -> Get-Content Alias cd -> Set-Location Alias chdir -> Set-Location Alias clear -> Clear-Host Alias cp -> Copy-Item Alias diff -> Compare-Object Alias dnsn -> Disconnect-PSSession Alias echo -> Write-Output Alias kill -> Stop-Process Alias ls -> Get-ChildItem Alias man -> help Alias mount -> New-PSDrive Alias pushd -> Push-Location Alias pwd -> Get-Location Alias rm -> Remove-Item Alias tee -> Tee-Object (...) So e.g. ls | Get-Member works too. The cmdlets however are often more powerful than their bash/Unix equivalent and - again - they work with objects, which you can always inspect if you're not sure what properties and methods they expose.