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.
Working with Drives
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:
- Registry (HKLM: or HKCU:)
- Certificate store (Cert:)
- Environment variables (Env:)
- Aliases (Alias:)
- Functions (Function:)
- Variables (Variable:)
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.
PS HKLM:\> DIR
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.
PS HKLM:\system> CD CurrentControlSet\Control
PS HKLM:\system\CurrentControlSet\Control> Get-ItemProperty CrashControl
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\system\CurrentControlSet\Control\CrashControl
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\system\CurrentControlSet\Control
PSChildName : CrashControl
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
AutoReboot : 1
CrashDumpEnabled : 2
Overwrite : 1
LogEvent : 1
DumpFile : C:\Windows\MEMORY.DMP
MinidumpDir : C:\Windows\Minidump
All in all it's more or less the same working with Aliases or Environment variables. Note the following difference in output, though:
PS Env:\> $Env:PROCESSOR_ARCHITECTURE
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:
PS C:\> Name = "Johan"
The term 'Name' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:5
+ Name <<<< = "Johan"
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:
PS C:\> $str2 = "World!"
PS C:\> $strCombo1 = "$str1 $str2"
PS C:\> $strCombo2 = '$str1 $str2'
PS C:\> $strCombo1
PS C:\> $strCombo2
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:
PS C:\> $arr2 = "A", "B", "C"
PS C:\> $arr3 = $arr1, $arr2
PS C:\> $arr3
PS C:\> $arr3
Finally, to get dynamic user input you can use the Read-Host cmdlet.
Okay, so to summarize we have now covered the following:
- PowerShell uses system drives to access the registry, environment variables, etc.
- Variables are preceded with a $
- To access the value of a variable, simply type it's name.
- Arrays are comma-separated lists of variables
- User input can be handled by the Read-Host cmdlet
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.