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