Create Input Box PowerShell

Create Input Box PowerShell

Hello,

If you write script for your help desk or non-initiated PowerShell people, you might want to simplify the PowerShel tools you provide to them. You may still need them to input a username or a mail address as input for your script to run only on a targeted user/mailbox.

Create Input Box PowerShell – Old way

There is a way to create an input box in PowerShell and then use the content inserted in it in your script.

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$User = [Microsoft.VisualBasic.Interaction]::InputBox('Enter a user name', 'User', "$env:UserName")

You may noticed the “LoadWithPartialName” static method which is obsolete :

https://msdn.microsoft.com/en-us/library/12xc5368%28v=vs.110%29.aspx

Create Input Box PowerShell - Obsolete API

Create Input Box PowerShell – Obsolete API

Create Input Box PowerShell – New way

The new way of loading assembly os with “Add-Type” cmdlet, so here we go :

Add-Type -AssemblyName Microsoft.VisualBasic
$User = [Microsoft.VisualBasic.Interaction]::InputBox('Enter a user name', 'User', "$env:UserName")
Create Input Box PowerShell - New Way

Create Input Box PowerShell – New Way

Once the help desk guy clicked on “Ok”, the $User variable will contain the value that was in the textbox, and then you can apply some regex/validation on it, and then use it to act on Active Directory account or mailboxes.

You can combine this with a precedent article of mine, and then build some basic Graphical User Interface (GUI) for your help desk users.

You can also checkout my GUI tag to find more way to build GUI with PowerShell.

0 thoughts on “Create Input Box PowerShell

  1. Hi,

    Is it possible to manually increase the size (length) of the InputBox if the title is too long and cut?

    Thank you for your advice.

Leave a Reply