3 ms·
Well I haven't really used PowerShell, but if it emits objects, can those objects not be printed in some form? Is it not reasonable to think of objects as havi
by milesrout 10y ago
Well I haven't really used PowerShell, but if it emits objects, can those objects not be printed in some form?
Is it not reasonable to think of objects as having a description of some kind. Think of URLs, for example.
find -iname '*.c'
could emit file:// URLs rather than just filenames. Then anything can unambiguously understand the format of the output.
- pzone 10y agoYes, all Powershell objects can be cast to string ($strvar = [string]$var) or piped to Out-String.
- milesrout 10y agoBut 'casting to string' isn't very specific. Does casting to string mean that you always get a unique identifier for the object? Does it mean you always get a dump of the object's contents?
- pjc50 10y agoYou get a printout of the object - effectively calling its toString() method. Note that this is not a reversible nor standard process. That's one of the main downsides of object-pipeline systems; in UNIX you can stop and serialise a pipeline to a file, then unserialise later. But you can't do this with object pipelines in Powershell because the objects have to be "live".
- ygra 10y agoPowerShell itself ships with a large XML file that governs how certain types are displayed by default if none of the Format-* cmdlets are used, e.g. FileInfo objects are usually rendered in a table with just the most commonly used properties displayed, which makes the interactive use of Get-ChildItem (Aliases: dir, ls, gci) very similar to ls -l. Explicit piping to Format-Wide, or Format-List will override those defaults (they only apply if no formatting is specified). Sure, everything can be made into strings at some point, but I've very rarely felt the need to do so in PowerShell (and regularly see on Stack Overflow that it only leads into problems down the (pipe)line if people try).