CommandNotFoundException
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()
}
}