PowerShell "is not recognized as the name of a cmdlet": find out why it can't see the command
Fix PowerShell "is not recognized as the name of a cmdlet, function, script file": check the spelling, PATH, the .\ prefix for scripts and missing modules, step by step.
by OhSudo ·
PS C:\> terraform versionterraform : The term 'terraform' 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.PowerShell looked everywhere it knows, and didn’t find anything by that name. So the question is why it isn’t looking in the right place. There are four usual answers: a typo, a folder that isn’t in PATH, a script in the current folder, or a module that isn’t installed.
TL;DR
Get-Command terraform -ErrorAction SilentlyContinue # does it know the name?$env:Path -split ';' # where does it look?.\deploy.ps1 # a script here needs .\Get-Module -ListAvailable ActiveDirectory # is the module installed?1. A script in this folder
This catches everyone once. You can see the file, and PowerShell says it doesn’t exist:
PS C:\scripts> deploy.ps1deploy.ps1 : The term 'deploy.ps1' is not recognized ...For safety, PowerShell doesn’t run things from the current folder unless you say so. It even suggests this at the end of the message. Add .\:
.\deploy.ps1If that gives running scripts is disabled on this system, then this problem is solved, and the next one has started: see that article.
A path with spaces needs quotes, and also &, which means “run this”:
& "C:\Program Files\MyTool\tool.exe" --version2. A program that isn’t in PATH
PATH is the list of folders searched for programs. You installed a tool, and its folder isn’t in the list. Find the file first:
Get-ChildItem -Path "C:\Program Files", "$env:LOCALAPPDATA" -Filter terraform.exe -Recurse -ErrorAction SilentlyContinue | Select-Object FullNameTo try it in this window only:
$env:Path += ";C:\Tools\terraform"terraform versionTo keep it for your user:
[Environment]::SetEnvironmentVariable('Path', [Environment]::GetEnvironmentVariable('Path', 'User') + ';C:\Tools\terraform', 'User')Use 'Machine' instead of 'User', from an administrator window, to apply it to all users.
Open a new window afterwards. A window that’s already open keeps its old PATH, and that’s why a tool you’ve just installed often isn’t found until you do. For a service or a scheduled task, you may have to restart it, or the machine.
Don’t use setx PATH "%PATH%;...", which you’ll see in old guides. It merges your user and system values into one, and cuts the result off at 1024 characters, which can break your PATH.
3. A module that isn’t there
Cmdlets come from modules. The name has a verb and a noun, such as Get-ADUser, and it isn’t found because that module isn’t installed:
Get-Module -ListAvailable ActiveDirectoryGet-Command Get-ADUser -AllSome come with a Windows feature, and others from the PowerShell Gallery:
# Active Directory, on Windows 10 and 11Add-WindowsCapability -Online -Name Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0
# Active Directory, on Windows ServerInstall-WindowsFeature RSAT-AD-PowerShell
# From the GalleryInstall-Module Az -Scope CurrentUserBetween versions: Windows PowerShell 5.1 and PowerShell 7 keep modules in different folders, so a module can work in one, and not in the other. See which one you’re in:
$PSVersionTable.PSVersionSome old commands are gone from PowerShell 7 altogether, such as Get-WmiObject. Use Get-CimInstance instead.
4. A typo, or a Linux habit
Check the spelling with a wildcard, which also tells you what really exists:
Get-Command *firewall*Commands from another system are common here:
| You typed | On Windows |
|---|---|
sudo |
Open an administrator window (gsudo, or sudo on recent Windows 11, if turned on) |
grep |
Select-String |
touch |
New-Item file.txt |
which |
Get-Command |
export VAR=1 |
$env:VAR = 1 |
apt, yum |
winget |
5. It works by hand, and not in a task
Scheduled tasks and CI agents can run under another user, and they get that user’s PATH. In a script, use the full path. A command with a typo inside a script gives the error on that line, so read the line number in the message.
Warning
Don’t copy an .exe into C:\Windows\System32 to make this go away. It works, and it mixes your tools in with the system’s, which makes updates and security reviews harder. Add a folder to PATH instead.
Shell, we have a problem!
Similar, but different: running scripts is disabled on this system is the execution policy. Access is denied means it was found, and you’re not allowed to run it. On Linux, the same problem says command not found, and on macOS, see command not found: brew. If Install-Module itself fails with Unable to resolve package source, see that article.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.