none
Set Commandline with editable default for user input RRS feed

  • Question

  • Hi Scripting Guys,

    I got a question regarding user input in powershell.  

    My users should be able to input time and date ranges and I would like to save them some typing by putting a default value on the command line.

    That is they should see something like

    Enter the date:> 2015-10-17 06:30

    and be able to edit the date before confirming with return 

    Is this possible.

    Any help is greatly appreciated.

    Monday, October 19, 2015 7:13 AM

Answers

  • This is not a feature of PowerShell but you can do it with  VB.

    Add-Type -AssemblyName Microsoft.VisualBasic
    [Microsoft.VisualBasic.Interaction]::InputBox('Edit or accept the date','Get Date','2015-10-17 06:30')


    \_(ツ)_/


    • Edited by jrv Monday, October 19, 2015 7:28 AM
    • Proposed as answer by BASATI Monday, October 19, 2015 8:56 AM
    • Marked as answer by Bill_StewartModerator Monday, November 23, 2015 9:56 PM
    Monday, October 19, 2015 7:27 AM

All replies

  • This is not a feature of PowerShell but you can do it with  VB.

    Add-Type -AssemblyName Microsoft.VisualBasic
    [Microsoft.VisualBasic.Interaction]::InputBox('Edit or accept the date','Get Date','2015-10-17 06:30')


    \_(ツ)_/


    • Edited by jrv Monday, October 19, 2015 7:28 AM
    • Proposed as answer by BASATI Monday, October 19, 2015 8:56 AM
    • Marked as answer by Bill_StewartModerator Monday, November 23, 2015 9:56 PM
    Monday, October 19, 2015 7:27 AM
  • function CustomInputBox([string] $title, [string] $message, [string] $defaultText) 
    {
        $inputObject = new-object -comobject MSScriptControl.ScriptControl
        $inputObject.language = "vbscript" 
        $inputObject.addcode("function getInput() getInput = inputbox(`"$message`",`"$title`" , `"$defaultText`") end function" ) 
        $_userInput = $inputObject.eval("getInput") 
        
        return $_userInput
    }
    
    $userInput = CustomInputBox 'Get Date' 'Edit or accept the date' '2015-10-19 02:20'
    if ( $userInput -ne $null ) 
    {
     echo "Input was [$userInput]"
    }
    else
    {
     echo "User cancelled the form!"
    }
    But, I think jrv's reply simple and neat

    Naveen Basati

    Monday, October 19, 2015 8:56 AM
  • If you want console editable input, you can use EditV32 (32-bit) or EditV64 (64-bit) to edit the content of an existing environment variable. PowerShell script example:


    $Env:TESTVAR="This is a test"
    editv64 -p "Enter a value: " TESTVAR
    if ( $LASTEXITCODE -eq 2 ) {
      "You didn't enter anything"
      Remove-Item Env:TESTVAR
    }
    if ( Test-Path Env:TESTVAR ) {
      "You entered: $Env:TESTVAR"
      Remove-Item Env:TESTVAR
    }
    

    You can get EditV32.exe and EditV64.exe from here:

    http://www.westmesatech.com/editv.html

    Note that you can only use EditV32.exe and EditV64.exe from the PowerShell console. They won't work in the ISE or other hosts.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, October 20, 2015 2:50 PM
    Moderator