Marco wrote:(OT: PS seems like command prompt on steroids. Does it work well as an evolution of old batch files? From your example I even notice "real" for loops...)
Well, from the functional point of view it's a revolution. Technically, they don't share anything, I guess.
To me PS is like a bash on windows, you can do anything. I'm a minor user only, but I can list some stuff I like the most.
Object-based
Piping only seems to work like unix/linux shells where commands are connected by reading/sending text streams via stdout/in. But in fact, .NET-objects are passed! Once you have an object you can simply use the .NET-api on it. Though you sometimes need some small debug statements to get the nature of the invidual types returned by commandlets, this is so funny and powerful.
Just some Examples
Code: Select all
[string] $fileHome = $(Get-Item $filePathStr).DirectoryName;
Get-Item returns a FileInfoObject for the given path from which you can simply fetch the parent folder
Code: Select all
if ($aStr.EndsWith($anotherSring)) {...}
A string is a real .NET-string!
Arrays, lists, hashmaps - initializiation by short notation or via constructor. Variables can be made type safe.
Code: Select all
$script:aMap = @{};
[System.Collections.ArrayList] $script:aList = New-Object System.Collections.ArrayList(0);
Simple xml-processing
Code: Select all
[xml] $xmlData = (Get-Content $xmlFilePathStr);
Yep, that's the entire code for reading and returning an xml-document on which you can do anything with XPath.
You can even create your own .NET-types form PS, you have modern error-handling (try...catch) and so forth.
It's really worth a look at.
Cheers,
Filehero