If you do desktop programming in Windows there is a good chance you do it under the supervision of the .Net Framework … framework … chortle, chortle.
Given that .Net Framework supervision, in Windows, you can write very sophisticated desktop programs involving Windows GUI concepts such as dialog boxes and buttons etcetera, etcetera, etcetera (it’s been too long), with C# or VB.Net, and great .Net Framework web applications using ASP.Net, via the Visual Studio IDE.
All of these programming options above produce code that you compile, normally using the Visual Studio IDE to simplify the “nuts and bolts” of how you do this.
You can create .Net Framework GUI programs with scripting as well, and the method for doing this is called PowerShell.
Today we perform a “Hello World” program using Powershell in today’s tutorial, and to do this, we leant heavily on this tutorial webpage which also talks about how to integrate a Powershell script with WPF (Windows Presentation Foundation).
Today’s tutorial “presentation” is a video one, presented by webcamming the screen of the Windows desktop screen with this MacBook Pro’s Photo Booth application’s webcam … as much as anything because don’t want to malign Photo Booth … see Air Server on Mac Primer Tutorial regarding my doubts … because, for lots of things, it still is the best idea, close to hand (for a primarily Mac desktop user). The resolution could be better but we’ll flesh out more below with some code and commentary … like a transcript … should you decide video is too difficult to decipher (or is too big to download, because, it is big) …
- HelloWorld.ps1 … contents of in clipboard (via Notepad’s Edit->Select All Edit->Copy) as we enter the tutorial …
$window = New-Object Windows.Window
$window.Title = $window.Content = "Hello World of Powershell and WPF together."
$window.SizeToContent = "WidthAndHeight"
$null = $window.ShowDialog()
- Windows icon (at bottom left)->All Programs>Accessories>Windows PowerShell>Windows PowerShell … wait to see Powershell prompt
- Windows icon (at bottom left)->All Programs>Accessories>Windows PowerShell>Windows PowerShell ISE … see a scripting editor and supervisor
- Into the top part Edit->Paste
- Click green triangular Run button … does the right thing … a dialog box via scripting … similar to the dialog box in this tutorial’s picture … who’d have believed it?!
- File->Save as… HelloWorld.ps1
- Windows icon->Run … enter “powershell -sta”
- At prompt, type in …
Add-Type -assemblyName PresentationFramework
Add-Type -assemblyName PresentationCore
Add-Type -assemblyName WindowsBase
.HelloWorld.ps1
- …results in session dialog (with an error) as per …
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\Documents and SettingsOwner> Add-Type -assemblyName PresentationFramework
PS C:\Documents and SettingsOwner> Add-Type -assemblyName PresentationCore
PS C:\Documents and SettingsOwner> Add-Type -assemblyName WindowsBase
PS C:\Documents and SettingsOwner> .HelloWorld.ps1
The term '.HelloWorld.ps1' is not recognized as the name of a cmdlet, function
, script file, or operable program. Check the spelling of the name, or if a pat
h was included, verify that the path is correct and try again.
At line:1 char:17
+ .HelloWorld.ps1 <<<<
+ CategoryInfo : ObjectNotFound: (.HelloWorld.ps1:String) [], Co
mmandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Documents and SettingsOwner>
- In getting information about the error, type in …
get-help about_signing
- In solving the permission issue we’ve plumped (after a false lead trying to alter the “Block” on the Properties of the HelloWorld.ps1 file) for …
get-executionpolicy
set-executionpolicy remotesigned
- Windows icon (right click)->Open Windows Explorer … find where HelloWorld.ps1 is … right click … choose Open with… Powershell … now this works (as evidenced by the dialog box in this tutorial’s picture today, as would the “powershell -sta” idea work now)
Actually, thinking on it, when we say … “who’d have believed it?!” … perhaps anybody programming in VBA and many Internet Explorer web programmers with VBScript and others (like perhaps the Basic programming language users), but nevertheless, find a scripting solution bringing up dialog boxes and other GUI objects very impressive.
If you are looking for the end result of today’s work see this picture else wait for the (huge) download of this video.
If this was interesting you may be interested in this too.
13 Responses to Windows PowerShell Primer Tutorial