by diego_moita · 10y ago · 0 comments
Bash and Unix commands are only "intuitive" after years of practice; Powershell is not different. And it doesn't have any obligation of being equal to bash.
I see it this way, the great thing about Powershell commands is that I get back an object. The great thing about Bash commands is that I get back text. The object is great because I don't have to parse it. The text is great because I can parse it.
The downside of Powershell is that if I don't have technical expertise in regard to the object a command returns, I have to go read deep into a manual to figure how I can do something simple. The downside of Bash is that if I have deep technical expertise about the text that is returned, I still have to parse it as text to do something simple.
The high level abstractions of Powershell are better suited for dealing with known processes and contexts where I care about the semantics of the returned value. The low level abstraction of text is better for dealing with unknown processes and contexts where I don't really care about the semantics of the value.
Is it possible for Powershell to output the object as JSON so that you can see what you can read from the object?
At that point, I either know the name of the method I'm looking for or I don't. In the first case the conversion to JSON doesn't do me any good because I can just call the method directly using `object.method(value)`. In the second case, it doesn't do any good because I don't know the method I want.
Reflection looks more like this: http://stackoverflow.com/questions/9952391/use-get-member-fo...
dir | ConvertTo-Html
dir | ConvertTo-Xml
dir | ConvertTo-Csv
Things I don't like, or is a pain point for me, are those same objects. As far as I can tell, there isn't a better way to figure out what you actually are getting back from a cmdlet besides feeding output through Get-Member.
For example, what comes back from dir/ls/Get-ChildItem? Well that's a trick question because it depends on which "provider" you invoke. On a file system (dir somedirectory) you'll get back FileInfo or Directory info, for a registry hive (dir hklm:) you'll get a RegistryKey, for environment variables (dir env:) you get a DictionaryEntry, etc.
So while it is fantastic there is a general way to enumerate items, the docs have to be vague and list System.Object as what returns from Get-ChildItem and its various aliases, which isn't very useful for working with the results.
This makes forming non-trivial commands with pipelines fairly annoying because I have to feed each stage through Get-Member to even figure out what I have and what is now available; this strategy starts to break down if a pipeline stage feeds a loop, etc.
I'd love it if the ISE displayed what actual object type exists at every point of a pipeline, rather than the trial-and-error-by-Get-Member approach I've adopted. Or for there to be some better way to get the return type of a cmdlet.
PowerShell is a huge improvement over the old command prompt though.
If you look at the note properties that get added to the objects, they're consistent across providers:
PS C:\> (Get-Item c:\).PSPath
Microsoft.PowerShell.Core\FileSystem::C:\
PS C:\> (Get-Item env:username).PSPath
Microsoft.PowerShell.Core\Environment::username
PS C:\> (Get-Item HKLM:\software).PSPath
Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software
There's also some magic that you can do with tab completion to see the properties. For example, if you type...
(Get-ChildItem c:\ | sort).
...and hit tab, you can tab through the list of properties of the objects returned by Get-ChildItem when it's used on the FileSystem provider. (Don't ask me how the hell that works, though...)
The excessive verbosity and object safety is great for admin scenarios where you don't suddenly destroy your corporate email transition because of a VP with an apostrophe in her name. But at that point perhaps you should just use C# instead?
But for everything else it just looks nuts to outsiders. That's why even the MS developer groups are releasing command line tools cross-platform that behave like Unix apps, not PS monstrosities.
https://azure.microsoft.com/en-us/documentation/articles/xpl...
Powershell should be a feature that draws people from AWS. Instead it is seen as a drawback, even inside MS.
Learn some alias's if you don't like the Get-X style of functions.
gal * | more
Will list tons of alias's for you.
dir -recurse | where {-Not $_.PsIscontainer -AND $_.name -match "_"} | foreach { $New=$_.name.Replace("_"," ") Rename-Item -path $_.Fullname -newname $New -passthru }
EDIT: I purposely posted the first Google result. You obviously Googled as well as your code got it backward. But really, why isn't it this? It's worked since the 1980s. You'll dig it since it uses extra apostrophes.
rename '_' ' ' *That command does some extra stuff, which would make the bash equivalent also complicated.
This isn't even probably the smallest, somebody smarter than me could do better.
There's probably a PowerShell module that has your equivalent.