Windows PowerShell Primer Tutorial
โœ‚๐Ÿƒ๐Ÿพโ€โ™€๏ธ๐Ÿƒ๐Ÿผโ€โ™‚๏ธ
๐Ÿ“–

Windows PowerShell Primer Tutorial

Windows PowerShell Primer Tutorial

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.

This entry was posted in eLearning, GUI, Software, Tutorials, Visual Studio and tagged , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

13 Responses to Windows PowerShell Primer Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *