/errors/powershell/term-is-not-recognized-as-the-name-of-a-cmdlet
PowerShell: The term 'x' is not recognized as the name of a cmdlet, function, script file, or operable program
- tool
- PowerShell
- exit code
- 1
- versions
- Windows PowerShell 5.1, PowerShell 7.x
- os
- windows, linux, macos
The term 'Get-Foo' is not recognized as the name of a cmdlet, function, script file, or operable program. means PowerShell found nothing with that name: no alias, function, cmdlet from an available module, script or executable in PATH. Check the spelling with Get-Command, then install or import the module that provides the cmdlet, or add the program to PATH. A script in the current folder must be called as .\script.ps1.
Reproduce it
'"hello from script"' | Set-Content hello.ps1
foreach ($cmd in 'Get-Foo123', 'hello.ps1', '.\hello.ps1') {
try { & $cmd } catch { "$cmd -> $($_.Exception.Message)" }
}Output
Get-Foo123 -> The term 'Get-Foo123' 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. hello.ps1 -> The term 'hello.ps1' 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. hello from script
hello.ps1 fails and .\hello.ps1 runs: PowerShell does not search the current folder, unlike cmd. In an interactive console it also prints a suggestion to use .\hello.ps1.
Find the command
Get-Command Get-ADUser -All # is it anywhere on this machine?
Get-Command *mailbox* -Module ExchangeOnlineManagement
Get-Module -ListAvailable ActiveDirectory
$env:PATH -split ';'| Situation | Fix |
|---|---|
| Cmdlet from a module that is not installed (Get-ADUser, Get-MgUser, Connect-ExchangeOnline) | install the module or Windows feature (RSAT for ActiveDirectory) |
| Module installed for PowerShell 7 only, running Windows PowerShell 5.1 (or the reverse) | install it for that edition, or check $env:PSModulePath |
| Exchange Online cmdlet such as Get-Mailbox before connecting | Connect-ExchangeOnline first; in EXO v3 these cmdlets are created by the connection |
| Program installed, new window not opened | open a new terminal so it reads the new PATH |
| Function defined in another script | dot-source it: . .\functions.ps1 |
| Linux or cmd habit (grep, ifconfig, dir /s) | use the PowerShell cmdlet: Select-String, Get-NetIPAddress, Get-ChildItem -Recurse |
Call a program with spaces in its path
A quoted path is a string, not a command. Use the call operator:
& 'C:\Program Files\Tool\tool.exe' --versionWithout &, PowerShell prints the string instead of running the program.
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_command_precedence
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-command
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules