News for hackers. Stuff that matters.
Re:PowerShell is open sourced and is available on Linux by aidos on Thursday August 18, 2016 @12:06PM (#12313642)
Cool! I'm having a play with it and it seems to work really nicely. Does anyone know why the following commands don't produce the same output?

    get-childitem | ConvertTo-Csv
    get-childitem | ConvertTo-Json | ConvertFrom-Json | ConvertTo-Csv
It seems like, if there's a perfect underlying data model doing what's effectively an identity conversion shouldn't change anything.

Edit oooo, and all the linux stuff is available within it too. So you can run this to just augment your linux experience.

    ls | grep '_' | ConvertTo-Json
Re:PowerShell is open sourced and is available on Linux by Jayku1 on Thursday August 18, 2016 @03:17PM (#12315534)
ConvertTo-CSV converts each property into a single string. Rich object? Tough. You get a string. If the type does not support a nice string representation, you get type names, like "System.Collections.Generic.List`1[System.String]" in your CSV output.

ConvertTo-Json serializes in-depth ... properties that are actually rich objects become nested dictionaries instead of simple strings. For instance, the "Parent" which is a Folder object. Recursively, up to the depth allowed.

Anyway, when you convert back, you don't get actual FileInfo and DirectoryInfo objects, you get dynamic property bags. So those complex properties remain dictionaries ... which ConvertTo-CSV turns into strings in a very different way.

If you specify -Depth 1 when you ConvertTo-Json ... the resulting output would be very similar.

Re:PowerShell is open sourced and is available on Linux by philsnow on Thursday August 18, 2016 @12:10PM (#12313688)
I don't have a powershell handy, but one thing that might be causing changes is that json dictionaries usually don't have a defined order (parsing libraries are free to render the keys of a dictionary in any order); are the changes you're seeing just ordering changes?
Re:PowerShell is open sourced and is available on Linux by trevorsullivan on Thursday August 18, 2016 @02:37PM (#12315194)
If you have a Docker Host, you can simply run: docker run --rm -it trevorsullivan/powershell

To clean up the image after you're done:

docker rmi trevorsullivan/powershell

Re:PowerShell is open sourced and is available on Linux by new_hackers on Thursday August 18, 2016 @12:27PM (#12313891)
ls and grep are aliases in powershell, so not exactly the same, but pretty close

For your first question: you have converted types. When you did a ConvertTo-Json, you serialized the System.IO.DirectoryInfo type into JSON compatible data. When you ConvertFrom-Json you have a PCSCustomObject.

The conversion to CSV is different for the System.IO.DirectoryInfo than the PSCustomObject. This has to do with the sub-types (converting from objects into serializable JSON objects). It also appears to handle time strings differently as well.

Re:PowerShell is open sourced and is available on Linux by ygra on Thursday August 18, 2016 @03:06PM (#12315430)
ls is an alias for Get-ChildItem. However, grep is the native command in this case. While such things work to some extent, it has the problem of turning your objects into plain strings. Effectively the same as if you had run

    (ls | % Name) -match '_'
The fact that the file's name is its default string conversion can be pretty annoying when dealing with results, e.g. from gci -recurse where it's quiet important to retain the path to the file. So, careful with such things. On Stack Overflow I tend to scold people who reduce PowerShell objects to strings without good reason ;-)