I have been spending a lot of my night looking at the Windows Powershell, which was released yesterday. This is a very powerful tool with a lot of extensibility options, especially for C# developers.
Once you install the Powershell you can navigate to C:\WINDOWS\system32\windowspowershell\v1.0 to look at the install files. In here you will only find one sample but you will find the documentation on how to use the powershell. This documentation is also located in the start menu. One of the best features of powershell is the documentation that accompanies the install. This is one the reasons I enjoy using Microsoft products so much, they have some of the best documentation. In addition, you should also check out the links to the documentation on MSDN. Here are some helpful links on the powershell:
http://channel9.msdn.com/wiki/default.aspx/Channel9.WindowsPowerShellSampleScripts
http://msdn2.microsoft.com/en-us/library/ms714622.aspx
http://msdn2.microsoft.com/en-us/library/ms714636.aspx
http://msdn2.microsoft.com/en-us/library/system.management.automation.cmdlet.aspx
http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
http://www.microsoft.com/technet/scriptcenter/scripts/msh/default.mspx?mfr=true
Now that you have some links I will demonstrate some potentially useful information for controlling an instance of Internet Explorer from the powershell. Here are the steps that you can follow to create an instance of IE that you can control.
- Open the powershell
- type: $ie = New-Object -ComObject InternetExplorer.Application
It is that easy, now you have an instance of the Internet Explorer object stored in the $ie variable. The nice thing is that you do not actually have to make IE visible, but if you do you can type the following command:
$ie.Visible = $true
If you would like to navigate somewhere you can use the Navigate method on the object, like so:
$ie.Navigate(http://www.communityserver.org)
So now you should have an IE window opened to the Community Server homepage. You can control the instance itself as well as the document that was returned. To see a list of the available commands that the Document object supports you can run the following command to print the commands in a text file:
$ie.Document | Get-Member | Format-Table -Wrap -AutoSize | Out-File -FilePath c:\DocumentCommands.txt -Width 2147483647
Now you should be able to open DocumentCommands.txt and see a list of all of the available commands. There are few useful ones, such as seeing the size of the page or the html on the page. Here are a couple of those:
$ie.Document.innerHTML
$ie.Document.fileSize
You can even resize the window if you want and move it around the screen by using the Width and Left properties of the Internet Explorer object.
I see a lot of possibility here, this is an amazing tool, especially because you can write custom command-lets and providers using C#. In addition, you can also write scripts using the native powershell commands. Here is a simple powershell script I wrote to report the size of the Google homepage:
$ie = New-Object -ComObject InternetExplorer.Application
$ie.Navigate("http://www.google.com")
write-host "The size is "
write-host $ie.Document.fileSize
$ie.Quit()
Remove-Variable ie
As you can see, I closed down IE and destroyed its variable. This can be helpful to avoid overlapping variables.
From what I have seen so far, the powershell is an extremely sweet tool to have in your toolbox. You can do some amazing things with managing your computer that I have not explored completely. To learn more you really should read the User Guide and the Getting Started.