0

I can display a simple Visual Basic inputbox from a PowerShell script like this:

$null = [reflection.assembly]::loadwithpartialname("microsoft.visualbasic")
$input = [microsoft.visualbasic.interaction]::inputbox($question, "bla", $text)

However, the inputbox window does not get focus which remains with the PowerShell window.

Is there a way to give focus to the inputbox window?

1 Answer 1

1

You could focus the InputBox from a job.

For example:

$null = [reflection.assembly]::loadwithpartialname("microsoft.visualbasic")
$activateWindow = {
        $null = [reflection.assembly]::loadwithpartialname("microsoft.visualbasic")
        $isWindowFound = $false
        while(-not $isWindowFound) {
            try {
                [microsoft.visualbasic.interaction]::AppActivate($args[0])
                $isWindowFound = $true
            }
            catch {
                sleep -Milliseconds 100
            }
        }
    } 

$job = Start-Job $activateWindow -ArgumentList "Unique Title"
$input = [microsoft.visualbasic.interaction]::inputbox("What is your answer?", "Unique Title", "none")
Remove-Job $job -Force
Write-Host $input -ForegroundColor Yellow
Sign up to request clarification or add additional context in comments.

3 Comments

Did you try copying and pasting my code exactly as it is? If so, try placing sleep 5 at the beginning of the job ScriptBlock, and see if the InputBox gets focus after 5 seconds. If I place the sleep at the beginning of the scriptBlock, I am able to manually activate some other window after the InputBox appears, and the job will reactivate the InputBox after 5 seconds.
Hey! It worked now. Don't know what I did wrong the first time... Ta.
Figured it out now. It doesn't work in PowerShell 1, which we use on servers. It does work in PowerShell 2, which I use on my PC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.