Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

The Term ‘else’ is Not Recognized as the Name of a Cmdlet

September 11, 2023
in Blog, Powershell
0
ADVERTISEMENT

Table of Contents

else is Not Recognized as the Name of a Cmdlet

We’re writing a PowerShell script to join a client into AD. We want to print a message if the computer is joined in Domain in the else clause.

$domain = (Get-WMIObject -NameSpace "Root\Cimv2" -Class "Win32_ComputerSystem").Domain
if ($domain -eq 'WORKGROUP') {
    Add-Computer -DomainName 'duybao.me' -DomainCredential $creds -Restart
} 
else {
    Write-Host "Already in Domain" -ForegroundColor Yellow
}

But we get the following error when running the command directly from a PowerShell console:

PS C:\> $domain = (Get-WMIObject -NameSpace “Root\Cimv2” -Class “Win32_ComputerSystem”).Domain
PS C:\> if ($domain -eq ‘WORKGROUP’) {
>> Add-Computer -DomainName ‘duybao.me’ -DomainCredential $creds -Restart
>> }
PS C:\> else {
>> Write-Host “Already in Domain” -ForegroundColor Yellow
>> }
else : The term ‘else’ 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
+ else {
+ ~~~~
+ CategoryInfo : ObjectNotFound: (else:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

That’s because when dealing with not the PowerShell Integrated Console. PowerShell just throws the text in the editor into the terminal window.

With the PowerShell Integrated Console (such as Windows PowerShell ISE), it did something differently (sending a message behind the scenes with the full script to make sure the full script gets run). We can do this because it’s Integrated.

That’s why you got the error when run at the command prompt, but when run in a script it should work fine.

v0xGQfOTlu0MtchLCW0CvFP07Wtk12KBhGpS9bX6jZAMqXHucSQva6VKXg1Q

To fix it, we need to change the code a little bit. PowerShell seems to consider the else block as separate statement, not attached to if. Modified the code to use the end of the if and else in the same line.

$domain = (Get-WMIObject -NameSpace "Root\Cimv2" -Class "Win32_ComputerSystem").Domain
if ($domain -eq 'WORKGROUP') {
    Add-Computer -DomainName 'duybao.me' -DomainCredential $creds -Restart
} else {
    Write-Host "Already in Domain" -ForegroundColor Yellow
}

As you can see, now your script can be run from command prompt without any error.

cNNROBEviOsOGWTsByuWJ1Tg14juvbZNJzcrqrMAICCqOYnFyKchKgwpdl8s

And it should be run in script mode as well.

h8th7xLMQ8ByMxX32cK3sR9oNjeYmoZIVwX75cJHWl9BG85611Qx9YTij4eI

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Install Remote Server Administration Tools (RSAT) on Windows 10

Next Post

How to Install Remote Server Administration Tools (RSAT) on Windows 11

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory