Can I customise the 'not recognized as the name of a cmdlet' error in Powershell? Can I customise the 'not recognized as the name of a cmdlet' error in Powershell? powershell powershell

Can I customise the 'not recognized as the name of a cmdlet' error in Powershell?


Yes, you can intercept the CommandNotFoundException with a CommandNotFoundAction!

$ExecutionContext.InvokeCommand.CommandNotFoundAction = {  param($Name,[System.Management.Automation.CommandLookupEventArgs]$CommandLookupArgs)    # Check if command was directly invoked by user  # For a command invoked by a running script, CommandOrigin would be `Internal`  if($CommandLookupArgs.CommandOrigin -eq 'Runspace'){    # Assign a new action scriptblock, close over $Name from this scope     $CommandLookupArgs.CommandScriptBlock = {      Write-Warning "'$Name' isn't a cmdlet, function, script file, or operable program."    }.GetNewClosure()  }}