Subscribe

Get the Network Administrators tool pack

Subscribe to our newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them.

Click Here to get your free tools

Recent Posts

Search

Archives

Create a conditional batch file – Based on OS, or Computer Name

Post image for Create a conditional batch file – Based on OS, or Computer Name

by Steve Wiseman on July 13, 2011 · 16 comments

in BAT Files,Command Line,Free


.

I got a question from one of our customers this week,

“Is there a way to run profile generator based on its workstation name? We want cached mode activated for laptops but not desktops. Or can you advise how this can be determined with the batch file?”

Actually we have had a tool internally that we have used for a long time. I decided to clean it up, and add it to our free tools.

It is called “IsComputer.exe” and you can get it from our downloads page

Here is how it works. You can give it two conditions – The version of Windows, or the name of the computer.

Lets say I want part of my script to only run on computers that are not Vista and above (Windows 2003, XP, and 2000) and start with the name LAPTOP:

IsComputer /name:LAPTOP* /os:CLASSIC

Or, I want it to only run on Windows 2003 computers:

IsComputer /OS:Windows2003

Or, maybe just computers that have ‘server’ in their name:

IsComputer /Name:*server*

You can also get it to just run on your 64 bit computers like this:

IsComputer /Platform:64

Those are just a few options – a complete list of options can be viewed by running it from the command line.

How do we get it to work with our batch file?

If a condition matches, the program will return 1. If it does not match it will return 0. Here is how we can create a simple script that will output ‘hello’ if we have a match:

@echo off
IsComputer /OS:Classic /quiet /name:rack*
if ERRORLEVEL 1 goto RUN_SCRIPT
if ERRORLEVEL 0 goto EXIT_SCRIPT

:RUN_SCRIPT

REM Your conditional batch statements would go here

echo Hello

:EXIT_SCRIPT

This example script will output ‘hello’ if the computer is running Windows 2003, and the name of the computer starts with ‘rack’.

You could put the batch file commands you want to execute right where the “echo hello” statement is.

Got a question, or an idea for a free tool? Let me know by mailing support@intelliadmin.com

One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools

Related Articles:

{ 16 comments… read them below or add one }

1 OFF July 13, 2011 at 8:37 pm

Great as usual.
Thanks for sharing!

2 Josh Willis July 14, 2011 at 12:32 am

Great tool steve. I have a convoluted VBScript that handles this. I like your freebie better. Thanks!

3 Jen July 14, 2011 at 12:33 am

Hello Steve

Is there any way to make a check like this against a username or windows group?

4 Steve Wiseman July 14, 2011 at 12:34 am

Glad to hear it helped you out Josh.

5 Steve Wiseman July 14, 2011 at 12:34 am

Hello Jen. You might want to check out another free tool of ours:

http://www.intelliadmin.com/index.php/2010/11/check-group-membership-from-a-batch-file/

6 David D July 14, 2011 at 12:39 am

Keep those free tools comin Steve. Love the USB history viewer too 🙂

7 Steve Wiseman July 14, 2011 at 12:39 am

Hello David – glad you like the free tools. There will be more…still lots of stuff we are working on that we want to put out there.

8 miles July 14, 2011 at 10:30 am

Awesome. I was just looking for something like this. We use a naming convention for our computers, and I wanted to map printers based on the name of the computer in my logon scripts. This works perfect for me!

9 Steve Wiseman July 14, 2011 at 10:30 am

Glad to hear it Miles

10 Inger Paul July 14, 2011 at 10:31 am

Great tip as usual steve. Please keep it up

11 Mathew Jenkins July 14, 2011 at 9:56 pm

Steve, cool tool. Does this work on Windows 2000?

12 Steve Wiseman July 14, 2011 at 9:57 pm

Hello Mathew,

Yes it works fine on 2000 Workstation, and Server

13 Grant Wilcockson May 29, 2012 at 6:45 pm

Hi,
Would there be any chance you could add an extra feature to see whether the OS is 32 or 64 bit as well. Otherwise awesome tool.

Thanks
Grant.

14 Steve Wiseman June 8, 2012 at 10:22 am

Hello Grant,

Just added the option. Here is the syntax:

/platform:[32/64]

So for example, to match a 32 bit system it would look like this:

/platform:32

Also, we added support for Windows 8 server and client in the os version detection.

Get the new version from our download page. It should have v1.1 in the header and a copyright of 2012 when you run it from the command line

15 Jennifer S June 8, 2012 at 11:52 am

Thank you steve for this update. I am deploying a few x64 machines now and this really helps!

16 Haon October 28, 2014 at 9:41 am

When I run the IsComputer.exe file, nothing happens and I keep getting the invalid command error from the command prompt when I attempt to use the IsComputer command. How exactly are you suppose to implement the IsComputer.exe file?

Leave a Comment

Category Links - Windows Forum - Exchange Forum