How to create Popups with Powershell


  • Finding the ProgID
  • Create an instance of a COM object
  • Using Popup method
  • Improve it with a script
  • See it in action
  • Bottom Line

Component Object Model is a platform-independent, distributed, object-oriented system for creating binary software components that can interact, COM was replaced for .NET, despite this, it remains as a viable technology with important legacy system foundation.


Finding the ProgID

There’s a cmdlet in Powershell which can create instances either from .NET framework or COM Object.

ComObjects are identified by it’s ProgID, to create an instance of it, we use the New-Object cmdlet with the ComObject parameter and specify the ProgID of the application we want.

So, how do we discover the ProgID we need?

Like .NET framework, COM has classes and each class has an CLSID (class identifier). ProgID’s names are located in HKEY_CLASSES_ROOT\CLSID key registry.

To store all ProgID’s in a variable use this command:

$PROGID = dir REGISTRY::HKEY_CLASSES_ROOT\CLSID -include PROGID -recurse | foreach {$_.GetValue(“”)}

Fine, but the result returns a very long list, that’s why we’re going to create a filter for it.

I know that the ProgID I want has to do something with “shell script” so I’m going to look for those key words in our search.

#This command will filter the ProgID list, WScript.Shell will be listed in this search.
$PROGID | Select-String -Pattern shell -Include script

search

With this filter it becomes easier to find what we’re looking for, note that ‘shell script’ is an embracing term and the result will return above 10 ProgID’s, much better than hundreds though.

Pay Attention:

To specify the ProgID just write it’s name ignoring that “.1” 

Create an instance of a COM object

Like already mentioned, just specify the ProgID for the value of ComObject parameter from New-Object cmdlet to create it’s instance.

$PopUp = New-Object -ComObject WScript.Shell

#List the methods of the object
$PopUp | gm -MemberType Method

As usual, to call the method we use “object.method()” syntax.

$PopUp.Popup()

But this method requires arguments to work properly, you can’t create an empty popup, see the comments in the script below to learn how to specify them.

#Popup method arguments syntax: ("Message",0,"title",Icon+Button)

#Icon values:

#Stop = 16
#Question = 32
#Exclamation = 48
#Information = 64

#Button values:

#OK = 0
#Ok/Cancel= 1
#Abort/Retry/Ignore = 2
#Yes/No/Cancel = 3
#Yes/No = 4
#Retry/Cancel = 5

#Pipeline return value:

#Ok = 1
#Cancel = 2
#Abort = 3
#Retry = 4
#Ignore = 5
#Yes = 6
#No = 7

$PopUp.Popup("How to create popups with Powershell",0,"FELIPETHEPOSHGUY",64+0)

Pay attention:

That ‘0’ right after the argument “message” doesn’t represents any property value, it just makes part of the syntax that separate the message location from title location.

Improve it with a script

Pipeline return value stands for that each type of button returns a specific value when clicked, that’s a good thing because we can make use of these returns to interact with the user choices, follow in the comments of the script below:

Of course we want to prompt these dialogs for a user, the problem is if we just run the Powershell script a Powershell window would appear and we don’t want our user to see that.
That’s why I’m going to run it through a Powershell shortcut which will run a hidden Powershell instance targeting the Popup file which contains the script above, even the powershell window style will be set to hiddenthe popups dialogs still appears.

It’s a good practice to set a ExecutionPolicy parameter with Bypass value in our shortcut, since there’s a huge probability that the user’s machine has it’s execution policy set to Restricted. The shortcut will not change the default value though, that’s even better because we won’t need to be worried about it afterwards.

How to properly set the shortcut:

#Syntax:
#[full path to Powershell.exe] -WindowStyle [value] -ExecutionPolicy [value] -File [path to powershell script file]

%systemroot%\system32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File C:\Users\Administrator\Documents\FELIPETHEPOSHGUY\Scripts\PopUp.ps1

See it in action!

Bottom Line

This is it!

Now is up to you to use your creativity, there are vastly possibilities with that, including creating functions and whatever more you imagine!

Thank you for reading, if you like this article please help me sharing it. 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.