Say I make a typo on the command line:
whih foo
Powershell returns:
whih : The term 'whih' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ whih mocha
+ ~~~~
+ CategoryInfo : ObjectNotFound: (whih:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The long message is useful for scripts, but for interactive shell use, I'd like to wrap it with something shorter, like:
'whih' isn't a cmdlet, function, script file, or operable program.
Can I wrap the error and change it to something shorter?
By default, you have to install modules in the exact order to use them. If that module is missing, corrupt, or got moved, it throws up the error, “the term is not recognized as the name of a cmdlet.” You can use “get-module” in PowerShell to see if the module is present and correct.
You can create an alias for a cmdlet, such as Set-Location . You cannot create an alias for a command with parameters and values, such as Set-Location -Path C:\Windows\System32 . To create an alias for a command, create a function that includes the command, and then create an alias to the function.
The term 'Select-Object-Property' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With