Conversation with PowerShell

by Oct 30, 2015

Today's tip is using the programmable CommandNotFoundHandler to have PowerShell talk with you once you enter an unknown command:

$ExecutionContext.InvokeCommand.CommandNotFoundAction =
{
  param(
    [string]
    $commandName,

    [System.Management.Automation.CommandLookupEventArgs]
    $eventArgs
  )

  $Sapi = New-Object -ComObject Sapi.SpVoice
  $null = $Sapi.Speak("I don't know $commandName, stupid.")
}

When you run this (and turn on your volume), then whenever the user enters an unknown command, PowerShell talks and complains that it does not know the command. You may hear the voice twice: if the command did not start with "get-", PowerShell first trys and finds a command that starts with "get-", followed by the entered name.

Twitter This Tip! ReTweet this Tip!