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 – 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")
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.
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.
I’m not sure if it’s possible.
I know this is an old post but I have found it useful, thank you. Do you know how to validate the user input though? I mean, the script just moves to the next step whether the user enters a value or not and clicks OK or Cancel. I need to determine that there is a value and the user clicked OK before some more script steps occur. I tried if ($User <> 0) {…..} but couldn’t get it to work.
You can try with:
if($User){…}
It will check if something was typed before clicking “Ok”.
[Microsoft.VisualBasic.Interaction]::InputBox($Message, $Title, $DefaultValue) | ?{$_ -ne $DefaultValue}