Skip to main content

Get the PowerShell V3–ShowWindow in PowerShell V2

This week I’m delivering a PowerShell class in Virginia to an audience who will be using PowerShell V2 for a while longer.  I noticed that one of the features of V3 that they like is the –ShowWindow parameter of Get-Help.  Since this class is a 10 day tool making course, I decided to just create the tool for them.  What I decided to do was to have Notepad display the help file information for me.  This provides the critical “Search” capability that they are looking for.  I also decided to address one of the short comings of Get-Help –ShowWindow and that is it’s inability to open multiple help windows at the same time.  Below is the code.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

Function Show-V2ShowWindow

{

[cmdletBinding()]

Param(

    [Parameter(Mandatory=$True,

    ValueFromPipeline=$true)]

    [String[]]$Cmdlet

    )

 

    Begin {}

 

    Process

    {

        # Generate a full path to the currently logged in user's "Documents"

        # Folder

        $Path = "$($Env:HomeDrive)$($env:HOMEPATH)\Documents\PSHelpFile.txt"

       

        ForEach ($C in $Cmdlet)

        {

            Try

            {

                # Collect the full Help File information for the cmdlet.

                Get-Help $C -Full -ErrorAction Stop |

                    Out-File -FilePath $Path

       

                # Open the txt file in Notepad

                Invoke-Expression -Command "Notepad.exe $Path"

               

                #Pause to allow Notepad to load before loading the next help file.

                Start-Sleep -Milliseconds 250

               

                # Remove the temporary txt file.

                Remove-Item -Path $Path

            }

 

            Catch

            {

                # If no help file exists for this cmdlet, then display a warning.

                Write-Warning "No Help file Available"

            }

        } # END: ForEach ($C in $Cmdlet)

    } # END: Process

 

    End {}

 

<#

.SYNOPSIS

Displays the full help file in notepad.

 

.DESCRIPTION

Displays the full help file in notepad.  This allows for multiple help files

to be open for users in PowerShell V2 environments to simulate

the functionality of -ShowWindow in PowerShell V3

 

.PARAMETER Cmdlet

The Cmdlet or ABOUT_ file that you want to display.

 

.EXAMPLE

Get-V2ShowWindow Get-Date

 

Displays the full help file for Get-Date in Notepad.

 

.EXAMPLE

Show-V2ShowWindow -cmdlet Get-date, Get-Command

 

Opens two help files at the same time.

 

.EXAMPLE

"Get-date", "Get-Command" | Show-V2ShowWindow

 

Opens two help files at the same time.  When piping cmdlets in from the command line,

they must be enclosed in quotation marks.

 

.NOTES

==============================================================================

Author: Jason A. Yoder

Company: MCTExpert of Arizona

 

User assumes are risk and liability for executing this code.  As with

all PowerShell code, read and understand it before executing it in your

environment.

 

Licensing: This code may not be republished or utilized in part or in full

for any for profit product or publication without the expressed written

consent of MCTExpert of Arizona. 

 

Copyright © 2014 MCTExpert of Arizona.  All rights reserved.

==============================================================================

#>  

} # End: Function Show-V2ShowWindow 

Here are a few areas of interest.

Line Comments
16 I need to store a txt file temporarily so I need a location where I know the user will have write access so I’m going to use there “My Documents” folder.  To find it, I’m using two variables from PowerShell’s Environment PSProvider, Homedrive and HomePath.  The value of these two variables help me correctly create the path to the temporary file.
18 Line 18 starts the ForEach loop that gives the cmdlet the ability to open multiple help files at one.
23 Get the help information and saves it to the temporary txt file.
27 This line will open Notepad.exe with the temporary file.
30 One problem that I noticed in testing is that if I am opening multiple files, the next command to remove the file executes before Notepad reads it.  To solve this problem, I pause the script for .25 seconds before allowing Remove-Item to clean up the txt file.
33 Removes the txt file.
36 Just in case the user asks for a help file that does not exists, this will prevent a blank Notepad window from opening and displays a warning message.

 

At the very least, I recommend that the script writer upgrades to at least PowerShell V3.  The ISE makes it work it and has features that are not available in the V2 ISE.  Remember that PowerShell V2 code is 100% compatible with V3 and V4 so if you are in an environment that has to wait to implement the newer versions of PowerShell, your tool making efforts will not be in vein.  Go ahead and script away.  They will work when you finally get to upgrade.

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.