This is a continuation of part I. If you haven't read it I suggest at least going through the summary at the end of the post.
In your basic command prompt you have the normal file system drives that you use to access your hard-drive, floppy, DVD, USB-stick, network shares etc. PowerShell uses this established interface to access other data sources as well. The following providers are accessible by default through drives in PowerShell 1.0:
When you install extensions for like the one fore IIS, etc. you usually get another drive accessible where you can look at the current webapplications and check their properties.
Selecting one of the PowerShell drives is easy. It is almost like in MS-DOS.
You can use the same commands as you'd do in MS-DOS, such as CD, DIR, MKDIR, etc.
To get a certain property for a registry key, (or a regular folder for that matter) you use the Get-ItemProperty cmdlet.
All in all it's more or less the same working with Aliases or Environment variables. Note the following difference in output, though:
Did you note how putting a $-sign before the variable name gave us the value of the variable instead of a complete object? This brings us quite nicely to the subject of variables.
Variables aren't that tricky to work with. They do not need to be pre-initiated. They do, however, require a $ to be identified as variables. Otherwise you get the following error message:
To display the value of a variable you simply type the name of the variable and PowerShell kindly returns the contents
When it comes to strings, you can choose for yourself if you want to use double quotes or single quotes, but there is one minor difference:
As you can see, a variable within double quotes will be evaluated, while within single quotes it will be interpreted literally.
Apart from strings you can also store integers and dates. Integers do not need delimiters and dates are most easily created using the Get-Date function
Declaring Arrays is a breeze. PowerShell will automatically Split any comma-separated sequence of values so creating a arrays is as easy as this:
The arrays are also dynamic by default, so adding values to them is a simple matter as well.
Creating two-dimensional arrays are done by declaring each row of the array as a one-dimensional array and then adding them to another array:
Finally, to get dynamic user input you can use the Read-Host cmdlet.
Okay, so to summarize we have now covered the following:
Next post will probably cover some script logic such as if-clauses, looping, etc. After that I think it will be time to post some sample scripts.