3 ms·
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-childite
by aidos 10y ago
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
- ygra 10y agols 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 ;-)
- philsnow 10y agoI 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?
- trevorsullivan 10y agoIf 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
- new_hackers 10y agols 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.
- Jayku1 10y agoConvertTo-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.