Author Posts

July 23, 2018 at 1:51 am

From anotheer blog I read an example of a simple GUI to prompt for user input.
It involves the InputBox function, which works almost like the Read-host.

$x = InputBox("Enter something >", "Title")

and whatever the user typed is stored in $x. This also includes [OK] and [Cancel] buttons.
When I tried it on my PS (v5.1) it came back with the error message that InputBox is not a valid function.
How can I obtain this function into my PS setup?
Are there equivalent cmdlets or functions for this simple requirement?

Would be grateful for any advice, tips or references.

July 23, 2018 at 3:08 am

This...

$x = InputBox("Enter something >", "Title")

Is not the wat to do this, at least not completely.

There are a plethora of ways to do this. It's just a matter of what you feel works for you.

What you are showing will throw errors, because it is not a PS cmdlet. It is some custom function someone wrote that is being called.

Yo can even use the PS help system to create input boxes like the below

function New-InputBox
{
    param
    (
        [Parameter(Mandatory)]
        [string]$FirstName,
        
        [Parameter(Mandatory)]
        [string]$LastName,

        [Parameter(Mandatory)]
        [string]$Password
    )

    $FirstName, $LastName, $Password
        
}
Show-Command -Name New-InputBox

To get a GUI input box, it really depends on what you are asking of the user. Meaning, passwords, computer name, username, etc.

To get a user password via a GUI, that is what

Get-Credential 

... is for, but there are other ways.

If you want a user name, computer name etc... Just use .Net direclty.

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$name = [Microsoft.VisualBasic.Interaction]::InputBox("Enter your name", "Name", "Lastname")
"Your name is $name"

Again, there are lots of samples of this PS GUI sort of thing all over the web, on youtube, ms virtual academy, ms channel 9, ms powershellgallery.com and forums like this. Just use your search engine to find them.

That OK/Cancel stuff is from the .Net approach, like this. Well, this is just a msgbox, without the input part.

Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::YesNoCancel
$MessageIcon = [System.Windows.MessageBoxImage]::Error
$MessageBody = "Are you sure you want to delete the log file?"
$MessageTitle = "Confirm Deletion"
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
'Your choice is $Result'

Or even this...

$d = [Windows.Forms.MessageBox]::show("PowerShell Rocks", "PowerShell Rocks",
[Windows.Forms.MessageBoxButtons]::YesNo, [Windows.Forms.MessageBoxIcon]::Question)

If ($d -eq [Windows.Forms.DialogResult]::Yes)
{
    'Yes, PowerShell Rocks'
}
else
{
    "On no, you don't like me"
}

If you want to do your own forms, and don't have the $$$ to pay for a designer tool, Sapien's PowerShell Studio – https://www.sapien.com/software/powershell_studio, there are free resources to do this, like https://poshgui.com or you can hard code it in for free in PowerShell_ISE (already on your computer by default, well, Windows) or Visual Studio Code (you have to download). Learning and using WPF (Windows Presentation foundation) for PS GUI development is not that hard and with the GUI drag and drop editors, those write the form code for you, but you still have to manually add the other code to make it do anything other than just display on your screen

So, again, lot's of ways to do this.

Just search for 'PowerShell GUI development', or 'writing PowerShell GUI' in you search engine of choice or on YouTube and you'll find a lot.

July 23, 2018 at 6:10 am

OK, sincere thanks ... this is very useful to me.